Jump to content

Recommended Posts

I am trying to make a drop down list form

the drop box gets its choices from 1 table, then when the user clicks submit the value gets stored into a variable $name and is then used in a query to output the data of another table. Everything works great except the default option of ALL.

 

I do not know what value to put in that will cause the query to return all records.

 

here is the code thanks in advance for any help you can lend...

 

//the query

$query_Cards = "SELECT Name, `Class`, FROM skill_cards WHERE skill_cards.`Class` LIKE '$name%' ORDER BY Name ASC";

 

//the form

<form id="form1" name="form1" method="post" action="">

  <label>Class

  <select name="select">

    <option value=" ">ALL</option>

    <?php

do { 

?>

    <option value="<?php echo $row_Class['Class']?>"><?php echo $row_Class['Class']?></option>

    <?php

} while ($row_Class = mysql_fetch_assoc($Class));

  $rows = mysql_num_rows($Class);

  if($rows > 0) {

      mysql_data_seek($Class, 0);

  $row_Class = mysql_fetch_assoc($Class);

  }

?>

  </select>

  </label>

<input type="submit" name="submit" value="submit">

</form>

 

Link to comment
https://forums.phpfreaks.com/topic/86436-solved-form-option-value-what/
Share on other sites

Well you can keep the option as 'All'. Purley becasue a user won't know the * means everything. Then where you process the form, you get the variable $name. You can check to see if it =='All'. If it == 'All' the $name = *. Then run your query using *. Else run the query using the value $name

I am sorry, I really should make a point of saying I am a noob in my post, kinda like my stupid sign....

 

Makes sense, but I havent touched statements yet..

 

I am thinking if I added a period to each entry I could have ALL = . in the first table

so as to fill $name with a . when ALL is chosen, then as long as each record has a period in it it should be returned and I wont have to learn if else then yet :)

 

Seems a fanagled way to go about it but should work yeah?

Thank you for your code example, but I am having trouble making it fit....

 

This is the query statment, along with some other things like the $name

 

//Database Cards Connect

$name = $_POST['select'];

mysql_select_db($database_Lw, $Lw);

$query_Cards = "SELECT Name, `Class`, Drops, Comments, Pic FROM skill_cards

WHERE skill_cards.`Class` LIKE '$name%' ORDER BY Name ASC";

$Cards = mysql_query($query_Cards, $Lw) or die(mysql_error());

$row_Cards = mysql_fetch_assoc($Cards);

$totalRows_Cards = mysql_num_rows($Cards);

 

could you help me put the puzzle together....

thanks again

try

 

$name = $_POST['select'];

If ($name == All)
{
$name = "*";
}

//Database Cards Connect
mysql_select_db($database_Lw, $Lw);
$query_Cards = "SELECT Name, `Class`, Drops, Comments, Pic FROM skill_cards 
WHERE `Class` LIKE '$name%' ORDER BY Name ASC";
$Cards = mysql_query($query_Cards) or die(mysql_error());
$row_Cards = mysql_fetch_assoc($Cards);
$totalRows_Cards = mysql_num_rows($Cards);


Ok that didnt work either.

So here is the whole code as it was prior to trying to insert if statement.

Thanks again for the help.

 

<?php require_once('../Connections/Lw.php'); ?>
<?php

//Database Class Connect
mysql_select_db($database_Lw, $Lw);
$query_Class = "SELECT `Class` FROM class_list ORDER BY id ASC";
$Class = mysql_query($query_Class, $Lw) or die(mysql_error());
$row_Class = mysql_fetch_assoc($Class);
$totalRows_Class = mysql_num_rows($Class);


//Database Cards Connect
$name = $_POST['select'];
mysql_select_db($database_Lw, $Lw);
$query_Cards = "SELECT Name, `Class`, Drops, Comments, Pic FROM skill_cards WHERE skill_cards.`Class` LIKE '$name%' ORDER BY Name ASC";
$Cards = mysql_query($query_Cards, $Lw) or die(mysql_error());
$row_Cards = mysql_fetch_assoc($Cards);
$totalRows_Cards = mysql_num_rows($Cards);

<body>
<form id="form1" name="form1" method="post" action="">
  <label>Class
  <select name="select">
   <option value="All">ALL</option>
    <?php
do {  
?>
    <option value="<?php echo $row_Class['Class']?>"><?php echo $row_Class['Class']?></option>
    <?php
} while ($row_Class = mysql_fetch_assoc($Class));
  $rows = mysql_num_rows($Class);
  if($rows > 0) {
      mysql_data_seek($Class, 0);
  $row_Class = mysql_fetch_assoc($Class);
  }
?>
  </select>
  </label>
<input type="submit" name="submit" value="submit">
</form>
<table border="1" cellpadding="5" cellspacing="5">
  <tr>
    <td>Name</td>
    <td>Class</td>
    <td>Drops</td>
    <td>Comments</td>
    <td>Pic</td>
  </tr>
  <?php do { ?>
    <tr>
      <td><?php echo $row_Cards['Name']; ?></td>
      <td><?php echo $row_Cards['Class']; ?></td>
      <td><?php echo $row_Cards['Drops']; ?></td>
      <td><?php echo $row_Cards['Comments']; ?></td>
      <td><?php echo $row_Cards['Pic']; ?></td>
    </tr>
    <?php } while ($row_Cards = mysql_fetch_assoc($Cards)); ?>
</table>
<br />
</body>
</html>
<?php
//the following lines of code,  does this mean when the page refreshes, or the submit button is clicked that the $name variable gets erased? if not what does it mean please?

mysql_free_result($Class);

mysql_free_result($Cards);
?>

 

Change

<?php
$query_Cards = "SELECT Name, `Class`, Drops, Comments, Pic FROM skill_cards WHERE skill_cards.`Class` LIKE '$name%' ORDER BY Name ASC";
?>

 

to

<?php
$where = '1'
if(strlen($name)){
  $where = "skill_cards.`Class` LIKE '{$name}%'";
}
$sql = "SELECT Name, `Class`, Drops, Comments, Pic FROM skill_cards WHERE {$where} ORDER BY Name ASC";

 

(edit)

And change

<option value="All">ALL</option>

 

to

<option value="">ALL</option>

FIXED!

 

I changed

$query_Cards = "SELECT Name, `Class`, Drops, Comments, Pic FROM skill_cards WHERE skill_cards.`Class` LIKE '$name%' ORDER BY Name ASC";

 

to

$sql = $query_Cards = "SELECT Name, `Class`, Drops, Comments, Pic FROM skill_cards WHERE skill_cards.`Class` LIKE '$name%' ORDER BY Name ASC";

echo $sql;

 

To get the query output to go run in phpadming to check it for errors (thank you for the tip!)

however adding $sql = seems to make the script work! I removed the echo and it still works, remove the $sql = and it breaks.

 

Thanks a ton I have been at this for 3 days....lol VICTORY IS MINE errrr or is it yours :)

Ok....but why would I want to?

Looks like you figured that out.

 

Remember to mark your topics as solved in the future.

 

Also, when debugging, you have to take steps to determine what is wrong.  You can only fix the problem if you have good information about what the problem is.  Echo statements are a quick way to track program execution paths, the data in variables, etc.  Just make sure you remove them when you're done.

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.