Jump to content
Krewe

If() = Echo " Selected"

Recommended Posts

Hello, I am currently trying to make a form that when edited will updated what the table on the front page shows.The way it will work is by updating a MySQL database. In my code for the form I want it to grab the "flavor" that is currently showing, and place it as the " selected" in the option field, so who ever edits it can see right there which flavors are already showing. Please note there are TWO different tables in the DB, one table holds ALL of the flavors, the other holds only 8 flavors that will show on the front page: Code:

<table><tr><td><h1>Change Flavors:</h1><br /></td></tr>    <?php while($count <= 8){	    echo "<tr><td><form>";	   echo "Flavor #{$count}:  ";    echo "<select name=\"in_store\">";    $flavor_set = get_flavors();    $in_store_set = get_flavors_in_store();    $in_store = mysql_fetch_array($in_store_set);    while($flavors = mysql_fetch_array($flavor_set)){	 echo "<option value=\"{$flavors['flavor_name']}\"";	 if($flavors['flavor_name'] == $in_store['flavor']){	  echo " selected";	 }	 echo ">{$flavors['flavor_name']}</option>";    }    echo "</select>";    echo "<input type=\"submit\" value=\"Edit Flavor #{$count}\"";    $count++;	   echo "</form><br /><br />";    echo "<img src=\"images/line.png\" width='500px' height='2px' /><br /><br />";	   echo "</td></tr>";    }?></table>

The table that holds all the flavors is $flavor_set, the home page table is $in_store_setget_flavors() and get_flavors_in_store() and two functions that I have set to get the info out of the DB. Thanks!

  • Like 1

Share this post


Link to post
Share on other sites

where are you facing the problem? what is not working as you expected?

Share this post


Link to post
Share on other sites

My problem is that I don't think my if statement is working properly.I want it to find which flavor is currently in Id = 1 in the table and show that flavor name in the Option area, (" selected").The way the while() loop works right now is, the $count is the same as the id in the $in_store_set table.So if the $count = $in_store['id'] how could you pull out the $in_store['flavor'] for that id and make it the default selected flavor? Here is a link to the form: Http://www.surfshackyogurt.com/testing3.0/staffindex.php

Share this post


Link to post
Share on other sites

Does the get_flavors_in_store function return a result with a single record, is there only one flavor in the store? The function name has "flavors" in it, but you're comparing against a single value instead of looking through more than one.

Share this post


Link to post
Share on other sites

The function returns the mysql_query, both functions do. Here they are:

function get_flavors(){  global $connection;  $query = "SELECT * FROM flavors";  $flavor_set = mysql_query($query,$connection);  confirm_query($flavor_set);  return $flavor_set;}function get_flavors_in_store(){  global $connection;  $query = "SELECT * FROM in_store LIMIT 8";  $in_store_set = mysql_query($query,$connection);  confirm_query($in_store_set);  return $in_store_set;}

I know the if statement will not work right now because it is looking for an instance when $flavors['flavor_name'] == $in_store['flavor']. The $in_store table has a total of 8 flavors, and always will. I was thinking, if I put in the while statement a code that got only the flavors whose 'id' was the same as the $count THEN I could compare the full list to the short list.I'll check if that works, but if you have a better idea please share. Thanks!

Share this post


Link to post
Share on other sites

Aha! Fixed it :)Here are the links to see what I wanted to do: Home Page Where Flavors are shown to public: Http://www.surfshack....com/testing3.0Form Page Where you can edit the home page flavors: Http://www.surfshack.../staffindex.php(The form does nothing right now so you can't edit squat :P) Here is all the code in case it helps you:

<?php while($count <= 8){  $in_store_by_count_set = in_store_by_count($count);  $count_set = mysql_fetch_array($in_store_by_count_set);	echo "<tr><td><form>";	   echo "Flavor #{$count}:  ";	echo "<select name=\"in_store\">";	$flavor_set = get_flavors();	$in_store_set = get_flavors_in_store();	$in_store = mysql_fetch_array($in_store_set);	while($flavors = mysql_fetch_array($flavor_set)){	 echo "<option value=\"{$flavors['flavor_name']}\"";	 if($count_set['flavors'] == $flavors['flavor_name']){	  echo " selected";	 }	 echo ">{$flavors['flavor_name']}</option>";	}	echo "</select>";	echo "<input type=\"submit\" value=\"Edit Flavor #{$count}\"";	$count++;	   echo "</form><br /><br />";	echo "<img src=\"images/line.png\" width='500px' height='2px' /><br /><br />";	   echo "</td></tr>";	}?>

Code for Functions:

 function get_flavors(){  global $connection;  $query = "SELECT * FROM flavors";  $flavor_set = mysql_query($query,$connection);  confirm_query($flavor_set);  return $flavor_set;}function get_flavors_in_store(){  global $connection;  $query = "SELECT * FROM in_store LIMIT 8";  $in_store_set = mysql_query($query,$connection);  confirm_query($in_store_set);  return $in_store_set;}function in_store_by_count($count){  global $connection;  $query = "SELECT * FROM in_store WHERE id={$count}";  $in_store_by_count_set = mysql_query($query,$connection);  confirm_query($in_store_by_count_set);  return $in_store_by_count_set;}

Thanks for the help guys.

Edited by Krewe

Share this post


Link to post
Share on other sites

Couple of errors selected should be selected="selected", and you are missing a closing angled bracket for a input <?php while($count <= 8){$in_store_by_count_set = in_store_by_count($count);$count_set = mysql_fetch_array($in_store_by_count_set);echo "<tr><td><form>";echo "Flavor #{$count}: ";echo "<select name=\"in_store\">";$flavor_set = get_flavors();$in_store_set = get_flavors_in_store();$in_store = mysql_fetch_array($in_store_set);while($flavors = mysql_fetch_array($flavor_set)){echo "<option value=\"{$flavors['flavor_name']}\"";if($count_set['flavors'] == $flavors['flavor_name']){echo ' selected="selected"';}echo ">{$flavors['flavor_name']}</option>";}echo "</select>";echo "<input type=\"submit\" value=\"Edit Flavor #{$count}\"/>";$count++;echo "</form><br /><br />";echo "<img src=\"images/line.png\" width='500px' height='2px' /><br /><br />";echo "</td></tr>";}?>

Edited by dsonesuk

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×