Jump to content

Recommended Posts

I have been trying to fix this code snippet because I need to do something different if my variable is empty.

$result = mysqli_query($link, "Select eldercatid from eldercategory where elderid = '$id'");
if (!$result)
{
$error = 'Error fetching elder details ' . mysqli_error($link);
include '../../includes/error.php';
exit();
}

while ($row = mysqli_fetch_array($result))
{
	$selected[] = array('eldercatid' => $row['eldercatid']);
}

 

here is the code being used on the page using the variable

foreach ($selected as $s);	
if ($ec['id'] == $s['eldercatid'])
{
echo 'selected = "yes"';
}

 

I need my $selected variable to be equal to anything if $row['eldercatid'] is blank or empty because on the next page I have a call to use $selected but I am getting an error that says undefined variable: selected.

 

I have tried all kinds of lines using if empty and if ($row['eldercatid'] != '') among other stuff but haven't had any luck.  I know this probably pretty basic stuff, I just don't know it well enough yet. 

 

Thanks for your help.

It means you havn't defined $selected as an array outside the while loop. Also your going about this the wrong way, you can simply check for a match inside the while loop instead of putting inside an array, or you can even do a simple

 

<?php
$result = mysqli_query("SELECT * FROM `eldercategory` WHERE `elderid` = '$id'");

while($row = mysql_fetch_array($result)){


	if($ec['id'] == $row['elderid']){

		// selected does equal to the $ec['id']

	}else{


	        // does not equal

	}	

}

?>

That was a terrible example.

 

<?php
$id = mysql_real_escape_string($ec['id']);
if ($result = mysqli_query("SELECT elderid FROM eldercategory WHERE elderid = '$id'")) {
  if (mysql_num_rows($result)) {
    // match found
  } else {
    // no match found
  }
}

 

?>

Thorpe, how can you quickly assume $ec['id'] is directly from an HTTP VAR and not inside a while loop for another query? I simply used he own code so that he may change it himself if that was the case. I understand the second code he posted was outside the while loop, but I just thought he went about it the wrong way. If i had made a terrible example, thank you for posting a better one, I always appreciate that and so do the members.

I think I may have been a little confusing.  Let me explain better.  I have 2 files, index.php and elders_modify.php

Here is the code from index.php as I have it right now.

$result = mysqli_query($link, "Select * from eldercategory where elderid = '$id'");
if (!$result)
{
$error = 'Error fetching elder details ' . mysqli_error($link);
include '../../includes/error.php';
exit();
}

while ($row = mysqli_fetch_array($result))
{
if($id != $row['elderid'])
{
$selected = '0';
}
else
{
	$selected[] = array('eldercatid' => $row['eldercatid']);
}

}

 

the $id is pulled when an admin clicks on a staff member to edit this code tells the elders_modify.php file which categories in the drop down menu to mark as "selected".  The code works fine when a staff member has a category assigned to them already.  If a staff member does not have a category assigned yet than $selected is empty because $eldercatid is empty.

 

here is the code from elders_modify.php

...
<td>Staff Category:</td><td><select name="eldercatid[]" multiple>
<?php foreach ($eldercat as $ec): ?>
<option value="<?php echo $ec['id']; ?>" 
<?php if ($action == 'elder_edit')
{

foreach ($selected as $s);	
if ($ec['id'] == $s['eldercatid'])
{
echo 'selected = "yes"';
}

}
echo '>';
echo $ec['category']; ?></option>
<?php endforeach; ?>
</select>

 

all I need to do is give $selected a value to carry over from index.php to elder_modify.php when a staff member has no category assigned yet.  For some reason my

$selected = '0';

isn't recognized.

Thorpe, how can you quickly assume $ec['id'] is directly from an HTTP VAR and not inside a while loop for another query?

 

It wasn't necessarily the usage of mysql_real_escape_string that I was harping on about. More the fact that you can (and should) do the actual comparison in your query.

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.