Jump to content

Passing an Array from a form and using it


jbradley04

Recommended Posts

I hope I can explain this but I am having a lot of difficulty with this.

So what I am trying to do is pass a list of users that someone picks on the form....

<input type="checkbox" name="who[]" value="{$results[nr].user_id}" /> This is through Smarty Templates loop

 

Next I am trying to process this info from PHP and pass it to smarty template.....

$who = $_POST['who'];

foreach($who as $x) {

$list = $x;

But before I pass the variable $list I am looking to do a query to get usernames

$query = mysql_query("SELECT username FROM users WHERE id = '$list'");

Then I will pass the variable $list and the respective username to tpl file...

At this point I guess I will do a foreach loop on SMARTY too....

 

I am not sure... I hope I explained that ok....

Any help would be greatly appreciated!

Thanks to all!

J

Not really...  What I put in the original post is all I have...

I am trying to take this array from a form

<input type="checkbox" name="who[]" value="{$results[nr].user_id}" />

and convert that into a variable so I can search Mysql

$who = $_POST['who'];

foreach($who as $x) {

$list = $x;

$query = mysql_query("SELECT username FROM users WHERE id = '$list'");

 

Just not working... I am sure I am way off but I think I need a for each command....

Thanks for your help!

Think about it, when you do

<?php
$who = $_POST['who'];
foreach($who as $x) {
   $list = $x;
}
?>

you're converting an array to one variable, which is not what you want to do. You probably want something like this:

<?php
$q = "SELECT username FROM users WHERE id in ('" . implode("','", $_POST['who']) . "')";
$query = mysql_query(q) or die("Problem with the query: $q<br>" . mysql_error());
?>

 

Ken

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.