Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

help with eval()

Status
Not open for further replies.

manicleek

Technical User
Jun 16, 2004
143
GB
I have a page that is taking values, seperated by a new line, in a database.
The values are id numbers for other articles in the db.

I have written code to take all the values in the db field and place them in an array.

The page has a list menu populated with every single article in the db, and I would like any articles that have the same id as one of the articles in the array to appear as already selected.

The array is working fine and I have echoed "$ifstatement" and that is fine.

The code is as follows:

Code:
<?
$i=0;
foreach(explode("\n", $editRS['internallinks']) as $intlinks) 
{ 
  $i++;
  $intlinkarray[$i] = $intlinks;
  if($i > 0) {
  }  
} 
?>

<select name="internal[]" size="15" multiple="multiple" id="internal[]">
<? 
while ($internalRS = mysql_fetch_array($internalresults)) { 
?>
<option value="<?= $internalRS['articleid'] ?>"  
<?
$j = 1;
$z = 1;

$articleid = $internalRS['articleid'];

$ifstatement = '$intlinkarray['.$z.'] == ' .$articleid;
	if ($i > 1) {
		while ($j < $i) { 
			$z++;
			$j++;
			$ifstatement .=	' || $intlinkarray['.$z.'] == ' .$articleid;				
				}
			}

if($i > 0) {
	if(eval($ifstatement)) { ?>
	selected="selected"
	<?  }
}				
?>
>
<?= $internalRS['articletitle'] ?> 
</option>
<? } ?>
</select>

I am getting a parse error on the "if(eval($ifstatement)) { ?>" line
 
First, doing this by eval() is an extremely bad idea. This is for two reasons: using eval() in this way makes for code that is hard to debug and modify; and I think eval() isn't going to work the way you want it to in this context.


If you want to check whether any of the elements of an array contains a particular value, I strongly recommend that you use in_array().

If you have to check only a limited range of the elements of an array, then something like:

Code:
<?php
.
.
.
$found = FALSE;
$start = 1;
$end = 3;
for ($counter = $start; !$found && $counter <= $end; $counter++)
{
   if ( $intlinkarray[$z] == $articleid)
      $found == TRUE;
}

if ($found == TRUE)
{
   /* do what you need done */
}
.
.
.
?>

Want the best answers? Ask the best questions!

TANSTAAFL!!
 
It would be to check all the elements in a mysql text field.

e.g. the id's would be in the field as

123456
654321
135791

etc

There could be no numbers in the field, or there could be a hundred. I need the code to search through each of these numbers and if any of the articles printed in the list menu match one of those in the db field then it will appear in the list box as already checked
 
Never mind, have found it

Code:
<?
$linkarray = explode("\n", $editRS['internallinks']);
?>

<select name="internal[]" size="15" multiple="multiple" id="internal[]">
<? 
while ($internalRS = mysql_fetch_array($internalresults)) { 
?>
<option value="<?= $internalRS['articleid'] ?>" <? if (in_array($internalRS['articleid'], $linkarray)) { ?>selected="selected"<? } ?>>
<?= $internalRS['articletitle'] ?> - <?= $internalRS['maincatname'] ?> - <?= $internalRS['subcatname'] ?>
</option>
<? } ?>
</select>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top