Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Selecting an item from a drop down box.
Old 03-13-2005, 07:36 AM Selecting an item from a drop down box.
Novice Talker

Posts: 5
Trades: 0
Here is an example of my code, ignore the format of the echo line at the bottom I was just testing that it displayed what I was looking for.

So just for clarity, I already have the site in html (http://www.mortgagehell.co.uk) but I want to use php to automate it more and so far its going well!

In this script what I want to acheive is to list all of the ID and heading field results in a drop down list, the user then selects which article it is they want to modify and I then pass the ID number to another script I have already prepared which will display the article from the database and allow it to be amended then saved back to the database.

I am confident on all aspects apart from being able to display the results of the ID and header in a drop down box and to allow the user to select a particular article for editing. I dont suppose the ID has to actually be displayed, it could be in a hidden field?

I think I am going along the right track?

Here is the sample code I prepared.

PHP Code:

<?

// Connecting
$connect = @mysql_connect('localhost''root''martin');
    if (!
$connect) {
        die( 
'<p>problem ' mysql_error() . '</p>');
    }

// Select Database
$select = @mysql_select_db('mortgage');
    if (!
$select) {
        die( 
'<p>problem ' mysql_error() . '</p>');
    }    

// Get data from tables

$res = @mysql_query("SELECT * FROM news"); 
    while ( 
$row mysql_fetch_array($res) ) {

        echo (
'<p>' $row['ID'] . $row['heading'] . $row['article'] . $row['author'] .$row['source'] .$row['articledate'] . '</p>');

    }

?>
wise1 is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 03-13-2005, 08:09 AM I don't know if this is the best way of doing that, or even if its what you wanted!
Junior Talker

Posts: 2
Trades: 0
Hi,
I dont know if this is what you are looking for in the dropdown box but this i think is how i would do it,

Code:
  
  <tr>
    <td>
      <select name="heading">
      <option value="" selected>Select a heading...</option>
      
<?php
  $sql = "SELECT ID, heading " .
         "FROM mortgage ORDER BY heading";
  $result = mysql_query($sql)
    or die("<font color=\"#FF0000\">Query Error</font>" .
           mysql_error());
  while ($row = mysql_fetch_array($result)) {
    if ($row['heading'] == $heading) {
      $selected = " selected";
    } else {
      $selected = "";
    }
    echo '<option value="' . $row['ID'] . '"' . 
         $selected.'>' . $row['heading'] . '</option>' . 
         "\r\n";
  }
?>
      </select>
    </td>
  </tr>
Im not totally sure if this is how its done (im really new to php) could someone check that out?
Also im actually having a very similar problem at the moment involving the code above,
How would i make this work if i didnt have the ID row directly relating the heading row. For example i have a database with a table of movies, each of which has a genre which is listed in the table movies as enum values. How can i select to show just the list of Genre's without a relating ID, as the table already has an auto incrementing ID relating to the movie. Is the only way to do this to make an extra table for the Genre's with there own ID's?

Thanks very much for your help,
Alex
alexlindley is offline
Reply With Quote
View Public Profile
 
Old 03-13-2005, 08:21 AM
Novice Talker

Posts: 5
Trades: 0
Thanks, I can see the logic in that, I will give it a go. Cheers.

Lee
wise1 is offline
Reply With Quote
View Public Profile
 
Old 03-13-2005, 12:21 PM
Novice Talker

Posts: 5
Trades: 0
A more leaner version that I have now used to get my desired result.

PHP Code:
$res = @mysql_query("SELECT * FROM news"); 
echo 
"<select name=\"newsID\">";
     while ( 
$row mysql_fetch_array($res) ) {
        echo 
"<option value=\"".$row["ID"]."\">".$row['heading']."</option>";
     }
echo 
"</select>";

echo 
"<input type=\"submit\" value=\"Edit\" name=\"edit\">"
wise1 is offline
Reply With Quote
View Public Profile
 
Old 03-13-2005, 12:29 PM
Junior Talker

Posts: 2
Trades: 0
Wise1,
Do you rekon there is a way to have a drop down from a mysql table where there is no ID relating to the information you want to put in the drop down?
Like for example on my movies page where i want to a have a drop down of all the possible genre's, however the ID is already being used for the movieID.
So therefore i dont seem to have anything to put in the <option value="???"> as i have no ID for the the genre's?

Any ideas?
Alex
alexlindley is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Selecting an item from a drop down box.
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.38104 seconds with 12 queries