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