Jump to content

Is there a way of list the content of a row in a drop down list (arrays maybye)


madspof

Recommended Posts

This is what I have done to populate a drop down menu in one of my files. I hope that is what you were asking....Snowdog

<select style="width:350px;" name="call_download">

<?

$query = "select * from call_reminder WHERE date >= '$start_date' AND date <= '$end_date' AND member_group = '$show_userdata->member_group' AND completed = '1' ORDER BY date DESC";

  $result_group = mysql_query($query) or die('Query failed: ' . mysql_error());							  
                                
  while($show_group = mysql_fetch_object($result_group))
  {
    $call = $show_group->call;
    $title = $show_group->title;
    $filename = $show_group->id;
    echo("<option value='$filename'>$call - $title</option>");
  }
  ?>

</select>

I have this code so far but i t does not seem to work

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<title>Untitled Document</title>

</head>

 

<body>

<?php

 

// Set the various database connect information.  Host is usually

// localhost.  If no password, just leave blank.  This information is 

// given by your web host OR if your own server, you setup yourself.

// See php manual for setup instructions.

$db_host  = 'localhost';

$db_user  = 'cms';

$db_pass  = '';

$db_name  = 'phpsms';

 

// More then one table can be used but in ths example we use just

// one. :-)  And in many scripts just one is used.  And it's easiest

// to comment on.

$db_table = 'userdata';

 

// ---- END CONFIGURATIONS ------------------------------------------//

 

// Make a connect to mysql.  Through this connect many databases

// and tables can be selected.  $conn will be what we call this

// connection.

$conn = mysql_connect($db_host,$db_user,$db_pass);

 

if ($conn == true) {

 

 

// Select the database.

  mysql_select_db($db_name,$conn); 

 

// We are selecting all fields (*) from database and using the $conn

// database connection.  By default $conn would be used but we're

// learning so may as well not be lazy :-)  Usually you wouldn't 

// select * but for the lazy and unsure, it works nicely.

//

// This is a very powerful function, one can query *any* SQL statement

// and do many things, like update/delete/insert/select data.  A good 

// basic SQL tutorial is : sqlcourse.com .  The result of this 

// query is named $result

  $result = mysql_query("SELECT * from $db_table",$conn);

 

// While a row exists to equal the fetched object (row) of data

// continue to loop until end of fetch (at the end)

// The way data is called may be new.  $row->dbfieldname . See 

// the manual on mysql_fetch_object() for more info.  Other functions

// can just as easily have been used, like mysql_fetch_array.  That method 

// one would use format as : $row['id']; , $row['name'] , etc.  See manual 

// for all the possible (and beautiful) tools that are available.

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

 

// These will loop through entire database.  $tmp keeps getting

// appended to so it's getting very large. This is what's printed

// and where one makes look pretty :-)  Below the database fields

// id, name and url are being printed from the $db_table table.  And, 

// the .= means it appends (concatenates) to $tmp so $tmp grows larger

// and later.  See : http://www.zend.com/zend/tut/using-strings.php

// Again, id,name and url are just examples ... replace according to 

// your database.

 

     

        $users[] = $row->userid;   

   

 

// The while loop ends here at this brace so the above cycles through until 

// all data is gathered.

    }

 

// Else we are not connected ($conn == false) so print error.

} else {

    echo 'could not connect to database : '. mysql_error();

}

 

// Print the pulled data.

?>

<form name="form1" method="post" action="message1.php">

  <p>

    <select name="select" size="1">

      <?

foreach($users AS $user){

    ?>

      <option>

      <?=$user?>

      </option>

      <?

}

?>

    </select>

  </p>

  <p>

    <input type="submit" name="Submit" value="Submit">

  </p>

</form>

<p> </p>

</body>

</html>

Well, firstly i would put my connection data in a different file, that can be easily included each time its needed

 

eg: db.php

<?php

$db_host  = 'localhost';
$db_user  = '*****';
$db_pass  = '*****';
$db_name  = '*****';

$conn = mysql_connect($db_host,$db_user,$db_pass) or die("Cannot Connect to database");
$db = mysql_select_db($db_name,$conn) or die("Could not select database"); 
?>

 

Then I would do the select box as below

 

<form name="form1" method="post" action="message1.php">
  
    <select name="selectUser" size="1">
<?	include "db.php";
	$result = mysql_query("SELECT * FROM userdata") or die(mysql_error());
	while($row = mysql_fetch_array($result)) {
		$userid = $row['userid'];
		$username = $row['username'];  // depends on what your field is called ?>
		<option value='<?=$userid?>"><?=$username?>

<?	} ?>

</select>
<br><br>
    <input type="submit" name="Submit" value="Submit">

</form>

After slight adjustment to the code u gave me i have this put am so close but so far :

</head>

 

<body>

<form name="form1" method="post" action="message1.php">

 

    <select name="selectUser" size="1">

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<title>Untitled Document</title>

 

</head>

 

<body>

Let's consider this. Out of all the replies on this thread 8 are yours and 3 are someone elses. That is spamming. Use the Modify button if you need to add/change something within 5 minutes.

 

Also use [ code ] tags bud. It makes it a lot easier to read through code.

okay i see ur point but if i dnt post another comment my post just gets moved back onto the other pages and is never seen again but okay i will stick to ur rule of dnt post anythink for 5 mins starting from this last post p.s i cannot et this dam code to work and ideas

 

editied:

I think my version of php will not acept the <? tage on it own it has to be <?php can u tell me a way of changing this

Only bump if a SIGNIFICANT amount of time has passed, or it IS on the second page. I have never seen this thread move off the front page and all of your other posts are within 5 minutes of each other. Believe me if someone has an answer for you it will be posted. But some people do not like helping other people who cannot read/obey the rules of posting here.

 

I would highly suggest reading the rule thread and following that if you want timely help from people. They tend to help others who obey more than not.

 

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
</head>

<body>
<?php 

// Set the various database connect information.  Host is usually 
// localhost.  If no password, just leave blank.  This information is  
// given by your web host OR if your own server, you setup yourself. 
// See php manual for setup instructions. 
$db_host  = 'localhost'; 
$db_user  = 'cms'; 
$db_pass  = ''; 
$db_name  = 'phpsms'; 

// More then one table can be used but in ths example we use just 
// one. :-)  And in many scripts just one is used.  And it's easiest 
// to comment on. 
$db_table = 'userdata'; 

// ---- END CONFIGURATIONS ------------------------------------------// 

// Make a connect to mysql.  Through this connect many databases 
// and tables can be selected.  $conn will be what we call this 
// connection. 
$conn = mysql_connect($db_host,$db_user,$db_pass); 

if ($conn) { 


// Select the database. 
  mysql_select_db($db_name,$conn);  

  $result = mysql_query("SELECT user_id from $db_table",$conn) OR DIE(mysql_error()); // incase that column name is wrong. 

while($row = mysql_fetch_object($result)) {   
        $users[] = $row->userid;    
     
    } 

// DEBUG STATEMENT FOR TESTING
print_r($users);

// Else we are not connected ($conn == false) so print error. 
} else { 
    echo 'could not connect to database : '. mysql_error(); 
} 

// Print the pulled data. 
?>
<form name="form1" method="post" action="message1.php">
  <p>
    <select name="select" size="1">
<option value="">Default</option>
      <?php
if (isarray($users)) {
foreach($users AS $user){
      print '<option value=' . $user . '>' . $user . '</option>';

}
}
?>
    </select>
  </p>
  <p>
    <input type="submit" name="Submit" value="Submit"> 
  </p>
</form>
<p> </p>
</body>
</html>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.