Jump to content

Assign to a variable?


Mr Chris

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/95795-assign-to-a-variable/
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/95795-assign-to-a-variable/#findComment-490431
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/95795-assign-to-a-variable/#findComment-490435
Share on other sites

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..

Link to comment
https://forums.phpfreaks.com/topic/95795-assign-to-a-variable/#findComment-490436
Share on other sites

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?

Link to comment
https://forums.phpfreaks.com/topic/95795-assign-to-a-variable/#findComment-490512
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.