Combining php variables<

liunx

Guest
Hi, I got 2 PHP questions

I decide to re-design my web site structure, here is what I have



<?php

for ( $iteration = 1; $iteration <= $number_table; $iteration ++ )
{

echo "<div align=\"center\"><center>";
echo "<table border=\"1\" cellpadding=\"0\" cellspacing=\"0\" style=\"border-collapse: collapse\" width=\"95%\">";
echo "<tr>";
echo "<td width=\"100%\" bgcolor=\"#808080\">$title_1</td>";
echo "</tr>";
echo "<tr>";
echo "<td width=\"100%\" bgcolor=\"#003300\">$content_1</td>";
echo "</tr>";
echo "<tr>";
echo "<td width=\"100%\" bgcolor=\"#808080\">$footer_1</td>";
echo "</tr>";
echo "</table>";
echo "</center></div><br>";

}

?>


this above code is in let's say a file call table.html
and on my index.php, I will use include command to include this file. I declare all variables in index.php already.
Here is what I need

I have these variables in index.php

$iteration
$title_1
$content_1
$footer_1
$title_2
$content_2
$footer_2
$title_3
$content_3
$footer_3

If you study the above code, you will see that the integer value of iteration sets how many table to be manipulate.
($iteration is desinated to take positive integer suchas 1, 2, 3...etc)

Now here is the problem
how do I combine $title_ with the variable $iteration, so when the for...loop is run for the first time, it will be $title_1, second time $title_2, third time $title_3, and for other variables?
Currently in my code, I put $title_1 there because I don't know how to combine it.

pleasse help thanks!use array! much simplier than variable variable
'cause you could use something like
$temp = 'title_'+$iteration
echo $$temp;
if i remember correctly

with array it's just
title[x]=value

you could also create multidimension array like this
page[0]['title']='title';
page[0]['content']='content';
page[0]['footer']='footer';
page[1]['title']='title2';
page[1]['content']='content2';
page[1]['footer']='footer2';

then you just have to replace the for with
foreach($page as $currentpage)
{
echo $currentpage['title'];
}

and so on!thank you so much!
I didn't realize that I can use array (I used to use array for everything in C++)
it works now
thanks again!!why can't you do this

for ( $iteration = 1; $iteration <= $number_table; $iteration ++ )
{

echo "<div align=\"center\"><center>";
echo "<table border=\"1\" cellpadding=\"0\" cellspacing=\"0\" style=\"border-collapse: collapse\" width=\"95%\">";
echo "<tr>";
echo "<td width=\"100%\" bgcolor=\"#808080\">$title_{$iteration}</td>";

or

$title_ . $iteration

but I agree with illogique, arrays are so much easier. that code I dispalyed can be problematic at best.hi
the 2nd way you post is the first thing I tried
it output

.1
.2
...
etc

but the first way works!
I didn't realize that I need a parenthesis () around the variable $iteration

so thanks for the tip!:Dthe second way has to be just like that. no quotes what-so-ever.

$new = $title_ . $iteration;

so it should combine them but hey, if you have it the first way then swell :)scoutt:
$new = $title_ . $iteration;

value of $title_ // not found
value of $iteration // number...
same as $new = $iteration


Michael:
it was not () it was {}which would make since now that you said it. I never tested it but that does seem right.
 
Back
Top