Jump to content

[SOLVED] array?


tgavin

Recommended Posts

I have a series of checkboxes (<input type="checkbox" name="foo[]">) that I need to get the names of and echo to the browser

[code=php:0]
foreach($_POST['foo'] as $foobar) {
// do database stuff to get the checkboxes corresponding names and id nums
$query = "select id,name from tbl where name = $foobar";
$sql = mysql_query($query,$conn) or die(mysql_error());
$row = mysql_fetch_assoc($sql);
$id  = $row['ln_id'];
$name = $row['ln_name'];
}

echo $name1;
echo $name2;
etc...
[/code]

Not sure how to do that?
Link to comment
Share on other sites

This is another way to do it:

[code=php:0]//find total records
$query = "SELECT * FROM your_table";
$result = mysql_query($query) or die('Query failed: ' . mysql_error());

if (mysql_numrows($result) <= 0){
echo "No Information Available";
}else{
// Printing results in HTML
$x = 0;
while ($x < mysql_numrows($result)):
$name = mysql_result($result, $x, 'name');
echo "<input name=\"foo[$x]\" type=\"checkbox\" id=\"foo[$x]\"/>$name<br />";
$x++;
endwhile;
}[/code]
Link to comment
Share on other sites

Each checkbox in the form has a number as its value. So when the form is processed and run through the foreach() loop I'm pulling the corresponding name from mysql. I then want to take that name and echo it later in a success message.

Everytime I run the loop, all that's retruned is the last pass of the loop. I'm just wondering how to capture that information from each iteration of the foreach() loop and (presumably) store it in an array for echoing later.
Link to comment
Share on other sites

i am thinking something like this:

[code=php:0]
$array = "";
foreach($_POST['foo'] as $foobar) {
// do database stuff to get the checkboxes corresponding names and id nums
$query = "select id,name from tbl where name = $foobar";
$sql = mysql_query($query,$conn) or die(mysql_error());
$row = mysql_fetch_assoc($sql);

$array[]  = array('id' => $row['ln_id'], 'name' => $row['ln_name']);
}
[/code]

That stores the id's and names in an array which you can call on later.

[code=php:0]
foreach($array as $v){
echo "ID is {$v[0]} and name is {$v[1]}<br>";
}
[/code]

i think i might have mucked up my array in there somewhere but I think thats what your looking for.
Link to comment
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.