How do I post two Arrays and query one of them?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

How do I post two Arrays and query one of them?

Post by simonmlewis »

This is in my form:

Code: Select all

	<td valign='top'>
			<input type='checkbox' name='id[]' value='$row->id' id='$row->id'>
        </td>
        
        <td valign='top' align='center'>
			<input type='checkbox' name='star[]' value='yes' id='$row->id'>
        </td>
The idea is if they tick the Star box as well as the ID box, then their comment is awarded a star.
At the top of the page, it does this:

Code: Select all

foreach($_POST['id'] as $id) 
    {
// magic happens
}
So how do I, within the foreach loop, query the star array, and see if there is a YES in the variable??
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: How do I post two Arrays and query one of them?

Post by Celauran »

Having a value of 'yes', how are you going to know which comment a star belongs to? I'd consider using the comment ID as the star value as well. You could then easily check whether or not a given ID is present in both arrays.

Code: Select all

foreach ($_POST['id'] as $id) {
    if (in_array($id, $_POST['star'], true)) {
        // Comment is checked and starred
    }
}
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How do I post two Arrays and query one of them?

Post by simonmlewis »

Great stuff, it works.
Thanks a mill.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
Post Reply