select box help

Discussion in 'PHP' started by Greenmethod, Aug 22, 2007.

  1. #1
    I am get a select box with the following items:
    Global
    Main
    Term2
    ...
    Term9

    The catch is that I want mysql to look through the database, and anything that is already in there I don't want to show in the select box. I have a pretty lengthy statement, but for some reason it's not working. Any help would be appreciated.

    		if (add == $_POST['add_edit']) 
    		{
    			echo '<select name="term">';
    			if ('Global' != $print_row['term'])
    				{
    				echo '<option>Global</option>';
    				}
    			if ('Main' != $print_row['term'])
    				{
    				echo '<option>Main</option>';
    				}
    			if ('Term2' != $print_row['term'])
    				{
    				echo '<option>Term2</option>';
    				}
    			if ('Term3' != $print_row['term'])
    				{
    				echo '<option>Term3</option>';
    				}
    			if ('Term' != $print_row['term'])
    				{
    				echo '<option>Term4</option>';
    				}
    			if ('Term' != $print_row['term'])
    				{
    				echo '<option>Term5</option>';
    				}
    			if ('Term' != $print_row['term'])
    				{
    				echo '<option>Term6</option>';
    				}
    			if ('Term' != $print_row['term'])
    				{
    				echo '<option>Term7</option>';
    				}
    			if ('Term' != $print_row['term'])
    				{
    				echo '<option>Term8</option>';
    				}
    			if ('Term' != $print_row['term'])
    				{
    				echo '<option>Term9</option>';
    				}
    			echo '</select>';
    		}
    PHP:
     
    Greenmethod, Aug 22, 2007 IP
  2. Eps

    Eps Peon

    Messages:
    17
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Well, the first thing I noticed is that starting from Term4, the if statement is the same for each term:

    if ('Term' != $print_row['term'])

    And secondly I don't understand how is it that "anything that is already in there I don't want to show " if all of the if statements compare against the same single value, $print_row['term']. From what I see, the script is only going to leave out a single item and print the rest.
     
    Eps, Aug 22, 2007 IP
  3. Greenmethod

    Greenmethod Peon

    Messages:
    112
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    woops! I've got that fixed!

    Maybe I can explain what i'm trying to do a little bit better..

    I have a shop that has 4 stations.

    The main computer prints to global printers, and term3 and term4 print to local printers.

    I have put the settings for global and term3 in the database already, but I need to add term4.

    When I click "add printer", I want the list to have Main, Term2, Term4, ... , Term9.

    I thought that those if statements would look through the database and if the term was already in there it wouldn't show that term in the select box.. do you follow me?
     
    Greenmethod, Aug 22, 2007 IP
  4. Eps

    Eps Peon

    Messages:
    17
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Well, either you've left out the piece of code that actually reads from the database or you have some misconception of interfacing with the database. I have never seen that it would be possible to look throught a database using $print_row['term'] (a request to get data indexed 'term' inside an associative array 'print_row').
     
    Eps, Aug 23, 2007 IP
  5. Greenmethod

    Greenmethod Peon

    Messages:
    112
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    sorry.. this is at the top of the page...

    $print_result = mysql_query("SELECT * FROM printers WHERE cust_no='$cust_no' AND term='$term'");
    $print_row = mysql_fetch_array($print_result);
    PHP:
     
    Greenmethod, Aug 23, 2007 IP
  6. Eps

    Eps Peon

    Messages:
    17
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    And yet still, it seems to me that it will only compare against a single value. $print_result may contain all the data you need, but a call to mysql_fetch_array alone will only return the first row. You might want to modify it so that it uses

    $terms = array();
    while($print_row = mysql_fetch_array($print_result))
    if(!in_array($print_row['term'], $terms))
    $printers[] = $print_row['term'];

    After this, $printers will contain all the terms the query found without any of them repeating. After this, you can simply replace all the if statements with calls to in_array. F.e.,

    replace
    if ('Main' != $print_row['term'])
    with
    if(!in_array('Main', $terms))
     
    Eps, Aug 24, 2007 IP