Jump to content

[SOLVED] Multiple Checkbox Query using text as values not numbers


Recommended Posts

so I've been looking to create a query based on multiple checkbox results (select results from multiple cities) and the only examples I've found so far use numbers as the values like the one below;

Page1

<?php
<form action="query.php" method="post" name="myform">
<tr>
<td><select name="name1[]" multiple>
<option value="value1">Value1</option>
<option value="value2">Value2</option>
<option value="value3">Value3</option>
?>

Page2

<?php
mysql_connect("host", "username", "password") or die(mysql_error());
mysql_select_db("db") or die(mysql_error());

$name1 = $_POST['name1'];
for($name1array=0; $name1array < sizeof($name1); $name1array++)
{
if($name1array < (sizeof($name1)-1)) { $name1_cond = " OR "; }
else { $name1_cond = ""; }
$name1q = $name1q."`name1` LIKE
'".$name1[$name1array]."'$name1_cond";
}

$name1q = "($name1q)";
$query = "SELECT * FROM `table` WHERE $name1q LIMIT 0,30";
?>

 

The problem I'm having is converting the FOR loop into something other than numeric. I'm using the actual city name as the value as it is relevant in the way the MySQL tables are setup. I'm sure it can be done but how? HELP!!! ~Rich

I prefer a while() loop, but what you have should work fine.  The for() loop isn't looking at the values of the items to determine how many times to loop, it's looking at a count of how many items are in the array (by using size_of(), the same as count()).

 

Regards

Huggie

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.