Jump to content

[SOLVED] Is this possible with a drop down and MySQL?


suttercain

Recommended Posts

Hi guys, and thanks to the people who helped getting my drop down working.

 

I was wondering if this is possible before I start trying it.

 

I have a drop down that is populated by MySQL and grouped by the column 'title'. So when you get the drop down you see all the titles:

 

Action Comics

Batman

Green Lantern

Superman

 

Now is this possible, to number the results for each title in the drop down...

 

EXAMPLE:

Action Comics (843 Issues)

Batman (7 Issues)

Green Lantern (23 Issues)

Superman (634 Issues)

 

That way before the user clicks the submit they now how many issues of that title is in the database?

 

Here is the code I am using for the MySQL generation:

<form action="comic_index_view.php" method="post">
			  <?php 
			  $result = mysql_query("SELECT title FROM comics GROUP BY title") or die(mysql_error());
				  $options="";
			  echo "<select name='title'>";
				  while($row = mysql_fetch_array($result)){
				  $title=$row["title"];
				  echo "<option value='" . $title . "'>".$title.'</option>';
					}
			  echo "</select>";
			  ?>
			  <input type="submit" name="submit">
			  </form>

 

 

Thanks for any answers or advice.

 

SC

Link to comment
Share on other sites

Duh....

 

I should have tried first

 

<form action="comic_index_view.php" method="post">
			  <?php 
			  $result = mysql_query("SELECT title FROM comics GROUP BY title") or die(mysql_error());
				  $options="";
			  echo "<select name='title'>";
				  while($row = mysql_fetch_array($result)){
			  $rows = mysql_num_rows($result);
				  $title=$row["title"];
				  echo "<option value='" . $title . "'>".$title. "(" . $rows . " Issues)</option>";
					}
			  echo "</select>";
			  ?>
			  <input type="submit" name="submit">
			  </form>

Link to comment
Share on other sites

Hi Barand,

 

Thanks I have been playing with what you wrote here and I know it is something I am missing and not catching on to.

 

I tried:

<?php 
			  $result = mysql_query("SELECT title, COUNT(*) as numissues FROM comics GROUP BY title") or die(mysql_error());
				  $options="";
			  echo "<select name='title'>";  
				  while($row = mysql_fetch_array($result)){
			  $rows = mysql_num_rows($result);
				  $title=$row["title"];
				  echo "<option value='" . $title . "'>".$title. " (" . $rows . " Issues)</option>'";
					}
			  echo "</select>";
			  ?>

 

And it still echos 363 Issues nect to each title.

 

So I removed the

$rows = mysql_num_rows($result);

and still had the same issue.

 

I also tried replacing numissues from the MySQL select stament with title and it changed the title "Action Comics" to the number of actual issues, which is what I am looking for without losing the title. So instead of reading Title Number of Issues it Reads Number of Issues #363. Also when I add the COUNT(*) and try to submit it, it messing up my results page and it doesn't echo anything.

Link to comment
Share on other sites

Chitty Chitty Bang Bang? = Impatience? I thought it was a light hearted reference to a Disney film. But who I am to know my own intentions and will?

 

None the less I thank you for your time Barrand, you led me on the right path and I was able to rectify the issue.

Link to comment
Share on other sites

Glad to hear you rectified the issue.

 

Just to ensure anyone looking at this thread is able to benefit from the solution, I'm assuming you solved the problem by using something along the lines of this:

 

<?php
   $result = mysql_query("SELECT title, COUNT(*) as numissues FROM comics GROUP BY title") or die(mysql_error());
   echo "<select name='title'>";  
   while($row = mysql_fetch_array($result)){
     $rows = $row["numissues"]; // Changed from $rows = mysql_num_rows($result);
     $title=$row["title"];
     echo "<option value='" . $title . "'>" . $title . " (" . $rows . " Issues)</option>'";
   }
   echo "</select>";
?>

 

;)

Link to comment
Share on other sites

This is what I did do get the results:

 

<form action="comic_index_view.php" method="post">
			  <?php
                  $result = mysql_query("SELECT title, COUNT(title) AS count FROM comics GROUP BY title") or die(mysql_error());
                  $options="";
                  echo "<select name='title'>";  
                  while($row = mysql_fetch_array($result))
				{                   
				$title=$row["title"];
                  echo "<option value='" . $title . "'>".$title. " (" . $row['count'] . " Issues)</option>'";
                    }
                  echo "</select>";
                  ?>
                  <input type="submit" name="submit">
                  </form>

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.