Jump to content

Recommended Posts

Hey,

 

I am wanting to make a script that will create a dropdown menu with different types of items (Shoes, Socks, Pants, ...) and how many items there are of that type (125, 44, 56, ...). I'm wondering if there is a way to check a mysql field for different several entries. Currently, I am using many queries like:

 

$sql1 = "SELECT * FROM items WHERE clothing='Shoes'";

$result1 = mysql_query($sql1) or die ("Couldn't get results.");

$num1 = mysql_num_rows($result1);

 

$sql2 = "SELECT * FROM items WHERE clothing='Socks'";

$result2 = mysql_query($sql2) or die ("Couldn't get results.");

$num2 = mysql_num_rows($result2);

 

$sql3 = "SELECT * FROM items WHERE clothing='Pants'";

$result3 = mysql_query($sql3) or die ("Couldn't get results.");

$num3 = mysql_num_rows($result3);

 

Is there a way to get all that done with one query? Is there a way to tell when the mysql field changes and then use a function? Thanks.

Link to comment
https://forums.phpfreaks.com/topic/241934-dynamic-drop-down-menu/
Share on other sites

Oops, I perhaps made that look simpler that I had meant to.

 

After getting the items from the database, I need to separate them as well. Which is why I had had them queried individually.

 

What I'm hoping to have is something similar to this:

 

<?php

 

$sql = "SELECT * FROM items";

$result = mysql_query($sql) or die ("Couldn't get results.");

$num = mysql_num_rows($result);

 

while($row = mysql_fetch_assoc($result)){

      $clothingtype = $row['clothingtype'];

      /// separate clothingtype into alike types (all pants, all socks, etc. together).

      /// should be 125 items with the value "shoes", 44 "Socks", 56 "Pants

}

 

echo "<form name='myform'>

<select name='option1' size='1'>

      <option value='$clothingtype1'>$clothingtype1 $numberofclothingtype1 items</option>

      <option value='$clothingtype2'>$clothingtype2 $numberofclothingtype2 items</option>

      <option value='$clothingtype3'>$clothingtype3 $numberofclothingtype3 items</option>

      .... etc";

 

You would 'remember' what the last value was using a variable ($last_type) and when you detect a change in the value, close any previous section and start a new section -

 

<?php

$sql = "SELECT * FROM items ORDER BY clothingtype"; // get the data you want in the order that you want it
$result = mysql_query($sql) or die ("Couldn't get results.");
$num = mysql_num_rows($result);

$last_type = NULL; // initialize to a value that will never exists as data
while($row = mysql_fetch_assoc($result)){

// detect if there is a change in the type
if($last_type != $row['clothingtype']){

	// detect if not the first section
	if($last_type != NULL){
		// this is not the first section, code to close out the previous section goes here...
	}

	// code to start a new section goes here...

	$last_type = $row['clothingtype']; // remember the new type
}

// code to output the data within the section goes here...
}

// detectif there were any sections at all and close out the last one if needed
if($last_type != NULL){
// code to close out the last section goes here...
}
?>

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.