Jump to content

[SOLVED] Problem | dropdown select list with table


frank13

Recommended Posts

Sup peeps,

 

I am having a problem with my code. The option i select don't work, it always the latest entry of the table that the value of the variable.

 

<form action="<?=$PHP_SELF?>" method="post" id="type">

<select name="category">

 

<?php

$sql="select * from client";

$result=mysql_query($sql);

 

while($ze = mysql_fetch_array($result))

{

$sub[] = $ze['type'];

}

 

$newData = array_unique($sub);

foreach ($newData as $dropDown)

{

?>

<option value="<? echo $dropDown?>"><? echo $dropDown?></option>

<?php

 

 

}

?>

<input type="submit" value="show" />

</select>

</form>

 

 

 

thx in advance.

 

peace

Try:

 

<?php

$categories=array();

$sql="SELECT * FROM client";

$result=mysql_query($sql);

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

$categories[]=$row['type'];

}

 

echo '<form action="' . $PHP_SELF . '" method="post" id="type">

<select name="category">';

 

foreach ($categories as $category){

echo "<option >$category</option>\n";

}

 

echo '

</select>

<input type="submit" value="show" />

</form>

';

?>

 

I think the main problem might have been your </select> was after the <input type...etc....>.  Anyways,  cleaned it up a bit, put it in order, and didn't break out of php so much.  This way it is cleaner and easier to read and hopefully you'll be able to understand it a little better.

Can i see your Insert code?  The block that inserts the value from the drop box into the database.  Actually, you're using $PHP_SELF, so it should all be the same page, so can you post all the code here. If it is over 100 lines use pastebin.

I am not trying do insert data in the table, I want to show a certain data :

 

code :


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<?php 

@include ("php/connect.php");
$PHP_SELF = $HTTP_SERVER_VARS['PHP_SELF'];
?>

<?php

$sql="select * from interior order by type asc";
$result=mysql_query($sql);

//$categories=array_unique();

while($row=mysql_fetch_assoc($result)){
$categories[]=$row['type'];
}

echo '<form action="' . $PHP_SELF . '" method="post" id="type">
<select name="category">';

foreach ($categories as $category){
echo "<option >$category</option>\n";
}

echo '
</select>
<input type="submit" value="show" />
</form>
';
?>

<br />
<?php 
echo "$category";
?>
<br />
<?php 



$result=mysql_query("select * from interior where type='$category'");
$i = 0;

while($row=mysql_fetch_assoc($result))

{
?>
<p><? echo $row['name']; ?><br />
<? echo $row['type']; ?></p>
<?php

$i+=1;
}
?>  
</body>
</html>


 

The only data selected is the lastest insert.

 

Where are you telling the difference between data and form?  You should an if statment like so:

 

if (!submit) {

 

code for showing the form

 

} else {

 

code for showing the chosen category

 

}

 

On top of that, you need to grab the form data when the page refreshes using $POST['category'];, assign that to $category, and go from there.  See if you can work with that.  if you run into more problems, post what you have and I'll sit down and write up some actual code.

I added below the code of the dropdown :

 <?php 


$select= isset($_POST['select']) ? $_POST['select'] : ''; 
?>



<?php

if (!empty($_POST['select']))

{

$sql = mysql_query("SELECT * FROM interior WHERE type='$select' ORDER BY ncontractor", $connection) or die("error querying database");
$i = 0;

while($result = mysql_fetch_assoc($sql))

...

{

 

The code was not working because a forgot to put $_POST  :-[ , now it works. By adding the if I don't get a error when nothing was selected. So for now all is working fine.

 

Thank for your help.

 

 

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.