Page 1 of 1
parse as php in a while loop array
Posted: Sun Nov 21, 2004 7:07 pm
by mrmbarnes
Hi All
I have a while loop and array. I want to run an if else statement in it to be used in the final result:
$result_array[] = "
if (".$_SERVER['REQUEST_URI']." == '/".$row[2]."') {
echo(' <img src='/images/2dots.gif' width='8' height='8' align='absmiddle'> <span class='whitetext'>".$row[1]."</span><br>
<img src='/images/spacer.gif' alt='' border='0' width='1' height='10'><br>
');
} else {
echo(' <img src='/images/2dots.gif' width='8' height='8' align='absmiddle'> <a href='".$row[2]."'>".$row[1]."</a><br>
<img src='/images/spacer.gif' alt='' border='0' width='1' height='10'><br>');
}
";
Where I want it displayed I am using the following in an echo statement:
eval('?>' . $result_final);
If I use my if/else statement by itself it works fine but as I am using it now it does not parse.
Can anyone help?
Many Thanks
mrmbarnes
Posted: Sun Nov 21, 2004 7:48 pm
by rehfeld
you need to use eval() properly
http://php.net/eval
but, eval is almost never the best way to solve a problem
the person who started php before it went open source, said:
"if eval is the answer, your almost certainly asking the wrong question"
id recomend you learn how to use functions, or devise another way to do it.
Posted: Sun Nov 21, 2004 7:51 pm
by mrmbarnes
Hi
I am using the eval statement:
eval('?>' . $result_final);
Do you have any other suggestion on how I could resolve this issue?
mrmbarnes
Posted: Sun Nov 21, 2004 7:54 pm
by rehfeld
we need more info, more code etc..
also, please use
Code: Select all
// psuedo code
while (row = sql_result) {
if (condition) {
echo "something $var";
}
}
Posted: Sun Nov 21, 2004 8:07 pm
by mrmbarnes
OK
Your help is greatly appreciated with this. I am trying to build a sidenav that will change the colour of the link depending on the url so you know which section you are in.
The sql I am using is:
Code: Select all
$result = mysql_query( "SELECT navid, text, link
FROM nav
ORDER BY navid asc
" );
The if/else php is:
Code: Select all
if ("".$_SERVER['REQUEST_URI']."" == '".$row[1]."') {
echo(" <img src='/images/2dots.gif' width='8' height='8' align='absmiddle'> <span class='whitetext'>".$row[1]."</span><br>
<img src='/images/spacer.gif' alt='' border='0' width='1' height='10'><br>
");
} else {
echo(" <img src='/images/2dots.gif' width='8' height='8' align='absmiddle'> <a href='".$row[2]."'>".$row[1]."</a><br>
<img src='/images/spacer.gif' alt='' border='0' width='1' height='10'><br>");
}
The full code I am currently using is:
Code: Select all
$result_array = array();
$counter = 0;
$PID = (int)($_GET['PID4']);
if( empty($PID))
{
$result = mysql_query( "SELECT navid, text, link
FROM nav
ORDER BY navid asc
" );
while( $row = mysql_fetch_array( $result ) )
{
$result_array[] = "
if ('".$_SERVER['REQUEST_URI']."' == '".$row[2]."') {
echo(' <img src='/images/2dots.gif' width='8' height='8' align='absmiddle'> <span class='whitetext'>".$row[1]."</span><br>
<img src='/images/spacer.gif' alt='' border='0' width='1' height='10'><br>
');
} else {
echo(' <img src='/images/2dots.gif' width='8' height='8' align='absmiddle'> <a href='".$row[2]."'>".$row[1]."</a><br>
<img src='/images/spacer.gif' alt='' border='0' width='1' height='10'><br>');
}
";
}
mysql_free_result( $result );
$result_final = "";
foreach($result_array as $page_link)
{
if($counter == $number_of_categories_in_row)
{
$counter = 1;
$result_final .= "";
}
else
$counter++;
$result_final .= "".$page_link."";
}
}
echo ("
<table width='100%' border='0' cellspacing='2' cellpadding='2'>
<tr>
<td>
<p>
<img src='/images/spacer.gif' alt='' border='0' width='1' height='10'><br>");
eval('?>' . $result_final);
echo ("</p>
</td>
</tr>
</table>
");
I need to learn more to get this going and your help is helping.
Mnay Thanks
mrmbarnes
Posted: Sun Nov 21, 2004 8:22 pm
by rehfeld
Code: Select all
if ($_SERVER['REQUEST_URI'] == $row[1]) {
echo "foo";
}