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
https://forums.phpfreaks.com/topic/35874-solved-array/
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
https://forums.phpfreaks.com/topic/35874-solved-array/#findComment-170113
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
https://forums.phpfreaks.com/topic/35874-solved-array/#findComment-170118
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
https://forums.phpfreaks.com/topic/35874-solved-array/#findComment-170175
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.