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

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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

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.