Friday, August 3, 2018

Problem solved for undefined offset notice when looping through arrays in php

Hello friends,

Today I am sharing very important code which is people searching online but solution is not available and that was asked in the following url of stackoverflow.com

https://stackoverflow.com/questions/3234616/undefined-offset-notice-when-looping-through-arrays-in-php/51672458#51672458

Please go with the below code, hope that will help you in your projects.

  <select title="mm" name="month" id="month" class="">
  <?php
  error_reporting(E_ALL & ~E_NOTICE);
  $datearray=array('Jan','Feb','Mar');
  for($d=0;$d<=sizeof($datearray);$d++){ ?>
  <?php if($datearray[$d]=='' OR $datearray[$d]==null) continue; ?>
           <option><?php echo $datearray[$d]; ?></option>
    <?php } ?>

  </select>


In the above code I have just updated two statement which is present in bold text.

- Error_reporting with (E_ALL & ~E_NOTICE) is important for removing notice error from whole page.
- Continue statement with code if($datearray[$d]=='' OR $datearray[$d]==null) continue; under for loop saying that if you will get blank or null value please continue whole loop without breaking anything.


Have any query in the above solutions, please put your comment in the below section.

Saturday, June 30, 2018

Combination of array and string functions in PHP

Hope below functions will help you in your current and coming projects for PHP language.

 Here we will talk about string and array functions and their usage.

String functions:
substr_count(): Count sub-string from given string.
Example:
<?php
echo substr_count("someone.good@gmail.com", ".");
?>
Result 2 because Dot (.) will be count in the above example.

str_replace(): Replace characters or words from the given string.
Example:
<?php
echo str_replace('Hello', 'Beautiful', 'Hello World!');
?>
Result:
Beautiful World!

preg_match(): It is used for matching pattern.
Example:
<?php
if(preg_match("/\bscripting\b/i", "PHP is the server side scripting language"))
echo 'A match was found';
else
echo 'A match was not found';
?>

Result:
A match was found

Array functions:
array_slice(): Extract values from given array.
Example:
<?php
$data = array('a', 'b', 'c', 'd', 'e');
$new_data = array_slice($data, 1);
foreach($new_data as $value){
    echo $value . "\n";
}
?>

Result:
b
c
d
e

in_array(): Check value exists or not.
Example
<?php
$data = array('football', 'bike', 'car');
$value = 'bike';
if(in_array($value, $data)) echo 'Yes I am present';
else 'Sorry I am not here';
?>

Result:
Yes I am present

array_flip(): Flip all the keys & values.
Example:
<?php
$arr = array('a'=>'A', 'b'=>'B', 'c'=>'C');
$flip_this = array_flip($arr);

foreach($flip_this as $key=>$value){
  echo 'Key:' . $key . ' and Value:' . $value . "\n";
}
?>

Result:
Key:A and Value:a
Key:B and Value:b
Key:C and Value:c



If you friends wanted to discuss here anything related to PHP functions and tips, please share those things. 

Wednesday, June 27, 2018

Various loop statement in PHP

Loop statements are useful for getting data from arrays. We will see while, do while, for & foreach loops. Without wasting time just go with the below examples and give me your suggestion for improvements.


While statement: This loop will execute while the condition is true.
example: suppose you will pass digit 5 in the text box then you will get character 'e' as a result, try below HTML and PHP code.

HTML code (file name: input-number.html)
<html>
 <body> 
  <form method="post" action="show-result.php">
  <div style="position:relative; margin:25% auto; width:300px;">
 
   <input type="text" name="number" placeholder="Put number & get character" style="width:230px;"> &nbsp; <input type="submit" value="Submit">
 
  </div>
  </form>
 </body>
</html>

PHP code (file name: show-result.php)
<html>
 <body>
<?php

 error_reporting(E_ALL & ~E_NOTICE);
 $input = $_POST['number'];
 $chars = array(1=>'a', 2=>'b', 3=>'c', 4=>'d', 5=>'e');

 $result = '';

 while(list($key, $value) = each($chars)){
  if($input!=null && $input==$key)
   $result = '<div style="color:red; font-size:22px; font-weight:bold; width:150px; margin:25% auto; border:1px solid black; padding:20px; text-align:center;">' . $value . '</div>';
  if($input!=null && $input!=$key)
   $result = '<div style="color:red; font-size:22px; font-weight:bold; width:150px; margin:25% auto; border:1px solid black; padding:20px; text-align:center;"> Sorry no records!</div>';
 }

 echo $result;

?>
 </body>
</html>

For loop: When you know in advance how many times the loop will run.
Example: In the below code you will get result between 1 to 10.
<?php

for($i=1; $i<=10; $i++){
echo $i . "<br>";
}

?>

Do while: Loop will run at least one time even if the condition is false.
Example: Below code will give you result 11 and not execute number greater than 11.
<?php
$x=11;

do{
 echo $x . "<br>";
 $x++;
}while($x<=10);

?>

Foreach: It is used to get the result in key, value pair for better understanding take a look in the below example:

<?php

$cars = array('Toyota', 'BMW', 'Ferrari', 'Audi');

foreach($cars as $key=>$value){
 echo 'Car on position <b>' . $key . '</b> is: ' . $value . '<br>';
}

?>

Result:
Car on position 0 is: Toyota
Car on position 1 is: BMW
Car on position 2 is: Ferrari
Car on position 3 is: Audi

Tuesday, June 26, 2018

Send push notification to multiple android devices using PHP & MySQL

Guys before starting this matter first create database and table then after that put 'server api key' (API_ACCESS_KEY) in PHP script, all the steps mentioned in the below.

How database & table looks I have updated screenshot just below


<?php
$con = mysqli_connect('localhost','root','','ottoedge');
if(!$con) die('Connection failed') . mysqli_connect_error();

$result = mysqli_query($con, "SELECT email, token FROM devices") or die('Unable to get FCM IDs' . mysqli_error($con));
$tokens = array();

while($row = mysqli_fetch_array($result)){
 $tokens[] = $row['token'];
}

#API access key from Google API's Console
define( 'API_ACCESS_KEY', 'AAAAfdCHJl8:APA91bFtHiNBibXcMVY9or0JCr2R15ez6jiAEpyt0tMVWdNu_AWin8sexxc5RLfJHW6VdZOce_2-QHvsmaPL5oAZPOYpJVnadFeL5kpM0eD6bzOyWI6kUdClum765xLNTN5ZukslsiRKS');

$registrationIds = $tokens;

#prep the bundle
     $msg = array(
  'body'  => 'Your notification for test drive reminder!',
  'title' => 'Alarm!',
              'icon' => 'myicon',/*Default Icon*/
               'sound' => 'mySound'/*Default sound*/
          );

$fields = array(
     'registration_ids' => $registrationIds,
     'notification' => $msg
);
$headers = array(
     'Authorization: key=' . API_ACCESS_KEY,
     'Content-Type: application/json'
);

#Send Reponse To FireBase Server
  $ch = curl_init();
  curl_setopt( $ch,CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send' );
  curl_setopt( $ch,CURLOPT_POST, true );
  curl_setopt( $ch,CURLOPT_HTTPHEADER, $headers );
  curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true );
  curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, false );
  curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $fields ) );
  $result = curl_exec($ch );
  curl_close( $ch );
#Echo Result Of FireBase Server
echo $result;

?>

Monday, June 25, 2018

How to create 'mobile friendly web page' without media query?

You can create 'mobile friendly web page' without media query. Yes its possible and below I have attached screenshot for desktop layout and pasted HTML code for your reference.



Create 'home.html' page, use below code and do the testing in mobile.

<!DOCTYPE html>
<html>
<head>
<title>Mobile friendly layout without media query</title>
<meta name="description" content="Creating HTML responsive page without media query. Please test on any mobile devices like Android, IOS, Windows...etc">
<style>
body{font-size:16px; max-width:1200px; margin:0 auto; padding:10px; overflow:auto;}
.logo{clear:both; color:black; text-align:center; padding:20px 5px;}
.logo a{color:black; text-decoration:none; font-size:40px;}
nav ul{
list-style-type:none;
padding:0;
margin:0;
overflow:hidden;
background-color:black;
text-align:center;
}
nav ul li{display:inline-block;}
nav ul li a{display:block; padding:10px; color:white;}
nav ul li a:hover{text-decoration:none;}

.container{clear:both; margin:20px 0; overflow:auto;}
.txt-div{float:left; margin:10px 5px; max-width:380px; min-height:100px; padding:5px;}
.txt-div::first-letter {font-size:200%; color:blue;}
footer{background-color:black; margin:20px auto;}
footer nav ul li a{color:white;}
</style>
</head>
<body>

<header>
<div class="logo"><a href="home.html" tile="digital marketing company">Code Lifo</a></div>

<nav>
<ul>
<li><a href="home.html">Home</a></li>
<li><a href="services.html">Services</a></li>
<li><a href="contact.html">Contact</a></li>
</ul>
</nav>

</header>

<div class="container">

<div class="txt-div">

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

</div>

<div class="txt-div">

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

</div>

<div class="txt-div">

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

</div>


<div class="txt-div">

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

</div>

<div class="txt-div">

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

</div>

<div class="txt-div">

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

</div>


<div class="txt-div">

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

</div>

<div class="txt-div">

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

</div>

<div class="txt-div">

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

</div>
</div>

<footer>
<nav>
<ul>
<li><a href="home.html">Home</a></li>
<li><a href="services.html">Services</a></li>
<li><a href="contact.html">Contact</a></li>
</ul>
</nav>
</footer>
</body>
</html>


I hope you found above basic HTML layout user friendly.
Have any suggestion or feedback, please share in the below comment section.