Mr Chris Posted March 12, 2008 Share Posted March 12, 2008 Hi Guys, In my code I echo out the following code as links if($i == $page){ echo($i." "); }else{ echo("<a href=\"$PHP_SELF?town=$town&from_price=$from_price&to_price=$to_price&bedrooms=$bedrooms&page=$i\">$i</a> "); } Which works great But instead of echoing I want to assign the code to a variable: if($i == $page){ $paglinks = ($i." "); }else{ $paglinks = "<a href=\"$PHP_SELF?town=$town&from_price=$from_price&to_price=$to_price&bedrooms=$bedrooms&page=$i\">$i</a>"; } and then output it: <?php echo $paglinks; ?> But this does not seem to output properly? Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/95795-assign-to-a-variable/ Share on other sites More sharing options...
kenrbnsn Posted March 12, 2008 Share Posted March 12, 2008 That should work. What is your script outputting? Please surround all code snippets with tags. Ken Quote Link to comment https://forums.phpfreaks.com/topic/95795-assign-to-a-variable/#findComment-490411 Share on other sites More sharing options...
MadTechie Posted March 12, 2008 Share Posted March 12, 2008 thats code seams fine.. whats problem with the output Quote Link to comment https://forums.phpfreaks.com/topic/95795-assign-to-a-variable/#findComment-490413 Share on other sites More sharing options...
porta325 Posted March 12, 2008 Share Posted March 12, 2008 Did you try instead of $paglinks = ($i." "); to put $paglinks="$i.\"\""; try that, might work. Quote Link to comment https://forums.phpfreaks.com/topic/95795-assign-to-a-variable/#findComment-490418 Share on other sites More sharing options...
Mr Chris Posted March 12, 2008 Author Share Posted March 12, 2008 Thanks Guys, Here's the code for both: Assigning to variable <?php for($i = 1; $i <= $numofpages; $i++){ if($i == $page){ $xx ="<div class='number'>".$i."</div>"; }else{ $xx ="<div class='number'><a href=\"$PHP_SELF?town=$town&from_price=$from_price&to_price=$to_price&bedrooms=$bedrooms&page=$i\">$i</a></div>"; } } //end for if(($totalrows % $limit) != 0){ if($i == $page){ $xx ="<div class='number'>".$i."</div>"; }else{ $xx ="<div class='number'><a href=\"$PHP_SELF?town=$town&from_price=$from_price&to_price=$to_price&bedrooms=$bedrooms&page=$i\">$i</a></div>"; } } ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>Untitled Document</title> <style> .number { float:left; display:inline; padding-top:4px; width:20px; height:20px; background:#0031AA; color:#FFFFFF; font-weight:bold; text-align: center; } </style> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> </head> <body> <?php echo $xx; ?> </body> </html> and the result here which does not output properly: http://www.thisiswindsor.com/search/n.php Echoing out <?php for($i = 1; $i <= $numofpages; $i++){ if($i == $page){ echo("<div class='number'>".$i."</div>"); }else{ echo("<div class='number'><a href=\"$PHP_SELF?town=$town&from_price=$from_price&to_price=$to_price&bedrooms=$bedrooms&page=$i\">$i</a></div> "); } } //end for if(($totalrows % $limit) != 0){ if($i == $page){ echo("<div class='number'>".$i."</div>"); }else{ echo("<div class='number'><a href=\"$PHP_SELF?town=$town&from_price=$from_price&to_price=$to_price&bedrooms=$bedrooms&page=$i\">$i</a></div> "); } } ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>Untitled Document</title> <style> .number { float:left; display:inline; padding-top:4px; width:20px; height:20px; background:#0031AA; color:#FFFFFF; font-weight:bold; text-align: center; } </style> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> </head> <body> <?php echo $xx; ?> </body> </html> and the result here which does not output properly: http://www.thisiswindsor.com/search/y.php Which works fine? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/95795-assign-to-a-variable/#findComment-490431 Share on other sites More sharing options...
kenrbnsn Posted March 12, 2008 Share Posted March 12, 2008 That's because you're overwriting the value of $xx in each loop. Either use concatenation or an array. I prefer using an array: <?php $tmp = array(); for($i = 1; $i <= $numofpages; $i++){ if($i == $page){ $tmp[] ="<div class='number'>".$i."</div>"; }else{ $tmp[] ='<div class="number"><a href="' . $_SERVER['PHP_SELF'] . '?town=' . $town . '&from_price=' . $from_price . '&to_price=' . $to_price . '&bedrooms=' . $bedrooms . '&page=' . $i . '">' . $i . "</a></div>"; } } //end for if(($totalrows % $limit) != 0){ if($i == $page){ $tmp[] ="<div class='number'>".$i."</div>"; }else{ $tmp[] ="<div class='number'><a href=\"$PHP_SELF?town=$town&from_price=$from_price&to_price=$to_price&bedrooms=$bedrooms&page=$i\">$i</a></div>"; } } ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>Untitled Document</title> <style> .number { float:left; display:inline; padding-top:4px; width:20px; height:20px; background:#0031AA; color:#FFFFFF; font-weight:bold; text-align: center; } </style> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> </head> <body> <?php echo implode("\n",$tmp); ?> </body> </html> Ken Quote Link to comment https://forums.phpfreaks.com/topic/95795-assign-to-a-variable/#findComment-490435 Share on other sites More sharing options...
MadTechie Posted March 12, 2008 Share Posted March 12, 2008 you need to append the data not re-set it <?php $x = ""; //add this for($i = 1; $i <= $numofpages; $i++){ if($i == $page){ //change to =. $xx =. "<div class='number'>".$i."</div>"; }else{ $xx =. "<div class='number'><a href=\"$PHP_SELF?town=$town&from_price=$from_price&to_price=$to_price&bedrooms=$bedrooms&page=$i\">$i</a></div>"; } } //end for ?> or as kenrbnsn says use an array.. Quote Link to comment https://forums.phpfreaks.com/topic/95795-assign-to-a-variable/#findComment-490436 Share on other sites More sharing options...
Mr Chris Posted March 12, 2008 Author Share Posted March 12, 2008 Thanks, but i'm getting an unxepected parse error: Parse error: syntax error, unexpected '.' on this line: $xx =. "<div class='number'>".$i."</div>"; // Output 'other pages' link for($i = 1; $i <= $numofpages; $i++){ if($i == $page){ $xx =. "<div class='number'>".$i."</div>"; }else{ $xx =. "<div class='number'><a href=\"$PHP_SELF?town=$town&from_price=$from_price&to_price=$to_price&bedrooms=$bedrooms&page=$i\">$i</a></div>"; } } //end for // Output 'first' image link if(($totalrows % $limit) != 0){ if($i == $page){ $xx =. "<div class='number'>".$i."</div>"; }else{ $xx =. "<div class='number'><a href=\"$PHP_SELF?town=$town&from_price=$from_price&to_price=$to_price&bedrooms=$bedrooms&page=$i\">$i</a></div>"; } } //end if I've tried googling the error, but it won't search for .= Can anyone post any links that will help me understand this better? Quote Link to comment https://forums.phpfreaks.com/topic/95795-assign-to-a-variable/#findComment-490512 Share on other sites More sharing options...
kenrbnsn Posted March 12, 2008 Share Posted March 12, 2008 The operation should be ".=" not "=." Ken Quote Link to comment https://forums.phpfreaks.com/topic/95795-assign-to-a-variable/#findComment-490518 Share on other sites More sharing options...
Mr Chris Posted March 12, 2008 Author Share Posted March 12, 2008 That's the one - thanks. Any links to this though? Quote Link to comment https://forums.phpfreaks.com/topic/95795-assign-to-a-variable/#findComment-490520 Share on other sites More sharing options...
kenrbnsn Posted March 12, 2008 Share Posted March 12, 2008 Did you try my solution using the array? Ken Quote Link to comment https://forums.phpfreaks.com/topic/95795-assign-to-a-variable/#findComment-490524 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.