Hi,
I am trying to save some entries in session.
i loop the values in while loop.
And then use session there.if i echo session inside loop,it prints correct entries.
but if outside loop,so i guess it will be 1 entry.which it is.
I want to store all entries in session and then can use outside while.
Is it possible.
i have this code:
<?php
$sql="select * from user where mobile !='' and sms = 'X'";
$query = mysql_query($sql) or die (mysql_error());
while($row = mysql_fetch_array($query)){
$mobile='';
if(ctype_digit($row['mobile']))//can also use is_numeric function
{
$mobile=($row['mobile']);
}
if (!empty($mobile))
{
$_SESSION['mob']= $mobile;echo $_SESSION['mob'];
//$_SESSION['mob']=$mobile;
//echo $max;
//echo "Your sms content: ".$max." ".$mobile;
}
}//echo "Your sms content: ".$max." ".$_SESSION['mob'];
//echo "<br>";
//$_SESSION['mob']= $mobile;
?>
Any ideas or alternative method to do this.
thanks
The fact is that your session variable gets overwritten everytime for as long as the while loop runs. Therefore only the last value from the loop is saved to the session array.
As an alternative you could use $_SESSION[‘mob’] as an array to hold all data related.
Could you please give an example for using and storing in Array.
session_start();
$_SESSION['counter'] = array();
for ($x = 0;$x < 5; $x++) {
$_SESSION['counter'][] = "The variable is :".$x;
}
var_dump($_SESSION);
Thanks,i appreciate your help.i have modified the code like this.
i echo the entries outside loop.Just need a little more assistance.
Code:
$query_table = "SELECT * FROM p_2_45 where NS != ''";
$result_table = $db->sql_query($query_table);
$_SESSION['max'] = array();
while($array = $db->sql_fetchrow($result_table)) {
//---- sselect individual piers (depends on coming data on time) cron will run in that resprective time ----------
$query_table2 = "select timeline,ChP1,id,timeline from s_2_45_test WHERE node_id='$array[node_id]' order by timeline desc";
$result_table2 = $db->sql_query($query_table2);
$array2 = $db->sql_fetchrow($result_table2);
$level = abs($array2['ChP1']);
$level_1 = $array['level_1'] + $array['offset'];
$level_2 = $array['level_2'] + $array['offset'];
$level_3 = $array['level_3'] + $array['offset'];
//------------ Set levels for sending sms ------------------------------------------
if($level > $level_3) {
$msg4 = 'level 3 '.$array['pier'].$array['NS'].' '.$array2['id']; $font = '#FF0000';
$_SESSION['max'][] = $msg4;
$sql="select mobile from user where mobile !='' and sms = 'X'";
$query = mysql_query($sql) or die (mysql_error());
$_SESSION['mob'] = array();
while($row = mysql_fetch_assoc($query)){
//assign each row to a new array element. This makes a 2d array
$_SESSION['mob'][] = $row['mobile']; // notice the [] on the var
//echo $_SESSION['mob']."<br>";
}
} else {
$msg5 = 'error '.$array['pier'].$array['NS'].$array2['id']."<br>"; $font = '#FF0000';
}
}
foreach($_SESSION['mob'] as $mob) {
//echo "$mob <br />";
}
foreach($_SESSION['max'] as $max) {
}//echo "$max <br />";
echo "Your sms content: ".$max." ".$mob;
echo "<br>";
The output is:
Your sms content: level 3 P119EN 1 012
Your sms content: level 3 P118EN 80 012
Your sms content: level 3 P119EN 1 017
Your sms content: level 3 P118EN 80 017
Your sms content: level 3 P119EN 1 018
Your sms content: level 3 P118EN 80 018
Your sms content: level 3 P119EN 1 019
Your sms content: level 3 P118EN 80 019
Which is not wrong but not 100% correct. If we look at the end of each record,there is mobile number.it mean that this code will send two sms to that person.which is wrong here.012 and so on are repeated twice for each record as P118EN,P119EN.
It should be like this:
Your sms content: level 3 P119EN 1 P118EN 80 012
Your sms content: level 3 P119EN 1 P118EN 80 017
Your sms content: level 3 P119EN 1 P118EN 80 018
Your sms content: level 3 P119EN 1 P118EN 80 019
And for this reason i ask for session in loop.
Now the above will send one sms and will keep record for each P118EN,P119EN.
Also i am trying to overcome on this problem since working on it from last two days.
To avoid duplication of data as you mentioned, you will need to check if the data is already in the array. The php function in_array does just that.
Please see the revised example:
session_start();
$_SESSION['counter'] = array();
for ($x = 0;$x < 5; $x++) {
if(!in_array($x,$_SESSION['counter'])) {
$_SESSION['counter'][] = "The variable is :".$x;
}
}
var_dump($_SESSION);
Thanks Ratna.i solved it.