Jump to content

Recommended Posts

I have a list, can be any length, and I need to be able to pull out a specific variable.

 

I had it working in a form but it was nested in another form, just learned that I cannot do that so I need help with how to get what I need done.

 

So say my list consist of 3 names

 

John

Jane

Betty

 

I click on a link to 'block' John from the list, what is the best way to do that?

 

This is what I had:

<form action='' method='POST' name='blockuser' id='blockuser'>
  <input type='hidden' name='block2' value='$sender' />
  <input type='image' value='Block' src='delete.png'  title='Block'>
</form>

if($_POST['block2']){
  echo "block $sender ";
}

 

I was thinking I need to put it in an array but this isn't working either

 

<input type='hidden' name='block2[]' value='$id' />
<input type='image' value='Block' src='delete.png'  title='Block'>

$my_array2 = $_POST['block2'];

if($_POST['block2']){
$totalIDs = count($my_array2);
for ( $i=0; $i < $totalIDs; $i++ ) {
	$sql2 = mysql_query("SELECT sender FROM nudges WHERE id='$my_array2[$i]'");
	while($r = mysql_fetch_array($sql2)) {
		$sender=$r['sender'];
		echo "block $sender ";
	} // END while
} // END for
} // END if($_POST['block2'])

 

what I get back is

block John block Jane block Betty

what I need is

block John

 

 

Maybe going with an array is not the way to do it, can someone help me out?

Link to comment
https://forums.phpfreaks.com/topic/256198-help-with-array/
Share on other sites

I'm guessing your FOR loop is just iterating all values in the list, which is why you're seeing all the values. But since I don't know how you're populating your block2[] array with values since it's a hidden type, I can't really tell how the values are getting into the array in the first place.

 

I just know that if you pass an array throw POST, it should contain all the values you put into it from the form and you should be able to iterate through that array with a FOREACH loop to get at those values.

 

Try this and tell me if it works or not (and what happens if it doesn't work).

 

<input type='hidden' name='block2[]' value='$id' />
<input type='image' value='Block' src='delete.png'  title='Block'>

$data = $_POST['block2'];

if($data)
{
   foreach ($data as $id) // changed FOR to FOREACH
   {
      $sql2 = mysql_query("SELECT sender FROM nudges WHERE id = $id;");
      while($r = mysql_fetch_assoc($sql2)) // changed mysql_fetch_array() to mysql_fetch_assoc
      {
         $sender=$r['sender'];
         echo "block $sender ";
      }
   }
}

Link to comment
https://forums.phpfreaks.com/topic/256198-help-with-array/#findComment-1313545
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.