Jump to content

[SOLVED] Session in while loop


mmarif4u

Recommended Posts

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

Link to comment
Share on other sites


<?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;		
}
}	

print_r($_SESSION['mob'])
?>

Link to comment
Share on other sites

Why are you checking what kind of data type your data is? You should not be doing that here. You should be doing that in the script that takes care of putting it into the database.  All data should be checked, sanitized, converted properly, etc.. BEFORE putting it into the database. I suppose checking if it's empty per row is okay, but it's not really ideal, especially if you're only making use of one column...

 

on that note, if you are only going to be making use of one column, only select that one column from the database.  There's no reason to burden the server with extra processing times and resource usage calling * like that.

 

You also do not need to be "reseting" mobile every iteration like that. In fact, you do not need that variable at all.  

 

Also, use mysql_fetch_assoc or mysql_fetch_row instead of mysql_fetch_array.  _array returns duplicate info: it returns a 2d array first element is numerical indexed 2nd element is associative.  There are very little instances in which you actually benefit from using _array.  It works, but it's overkill.  And even more overkill is that you can add a 2nd argument to _array to specify returning only 1 or the other, making it exactly like _row or _assoc.

 

<?php
$sql="select mob from user where mobile !='' and sms = 'X'";
$query = mysql_query($sql) or die (mysql_error());

while($row = mysql_fetch_assoc($query)){
  // assign each row to a new array element. This makes a 2d array
  $_SESSION['mob'][] = $row['mob']; // notice the [] on the var
}

// example to display info
foreach($_SESSION['mob'] as $mob) {
  echo "$mob <br />";
}
?>

Link to comment
Share on other sites

Thanks guys for reply.

i tried both suggestions.

Yeh Crayon you are right,y to put burden on the server,this code i am just using for testing.After finalizing the code i will modify it for using minimum resources like you mention.

Ok,now to the point:

I get this error while trying both solutions:

Fatal error: [] operator not supported for strings in C:\Program Files\Apache Group\Apache2\htdocs\monitoring_2\cron_sms.php on line 161
Link to comment
Share on other sites

You mean like this:

 

$sql="select mobile from user where mobile !='' and sms = 'X'";
$query = mysql_query($sql) or die (mysql_error());

while($row = mysql_fetch_assoc($query)){
   // assign each row to a new array element. This makes a 2d array
   $row['mobile']; // notice the [] on the var
$_SESSION['mob'] = array($row['mobile']);
}
//example to display info
foreach($_SESSION['mob'] as $mob) {
  echo "$mob <br />";
}

Link to comment
Share on other sites

whoa hey I notice you have this in that code you just posted:

   $row['mobile']; // notice the [] on the var
$_SESSION['mob'] = array($row['mobile']);

 

You didn't have that first line in there before did you? That could be your problem right there. You're trying to redefine $row when the while loop already does that in the condition. But regardless, I was saying like this:

 

$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'];
}
//example to display info
foreach($_SESSION['mob'] as $mob) {
  echo "$mob <br />";
}

 

If you had that $row['mobile']; in there before, simply removing that should work, without adding that line before the loop.

Link to comment
Share on other sites

Crayon Violent means like this....

<?php
$sql="select mob 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['mob']; // notice the [] on the var
}

// example to display info
foreach($_SESSION['mob'] as $mob) {
  echo "$mob <br />";
}
?>

Link to comment
Share on other sites

Thanks guys,i appreciate your help.that code works perfect.

Now i add that code inside another code which already in while loop.

and then echo the entries outside loop.Just need a little more assistance.

Code:

<?php
$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.

hope i am not overstating here.

Link to comment
Share on other sites

Thanks guys,i solved it without session and array.

Just define

$string="";

before while loop.

and then in the loop i did:

$string=$string.",".$msg;

It stores all the entries which are above the level.

something like which i define in the prev post.

 

Closed Topic.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.