Jump to content

LOOPING PROBLEM.. HELP PLS. :(


jushiro

Recommended Posts

I've made a loop here..

 

<form name="formdeliver" method="post" action="addOrder.php"  onSubmit="return confirm('Are you Sure the data inputed are correct? Click Ok to continue.')">
<div id="apDiv2" style="overflow:auto ">
<?php 
$host="localhost"; 
$username="root"; 
$password="";
$db_name="SEdatabase";
$tbl_name="bottlenumbers"; 

mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);

$name = $_SESSION['customername'];
$q = $_SESSION['quantity'];

echo "<table border='1'>
<tr>
<td><font size = '2'>Customer Name : $name </font></td></tr><tr>
<td><font size = '2'>Quantity : $q </font></td>
</tr>
</table><br>";


for($i = 0; $i < $q; $i++){
    echo "Bottle Number :";

echo "<select name=\"bottlenum[]\" onChange=\"this.name\"> 
    <option>Select</option>";

    if(mysql_num_rows($result)) 
    { 
    while($row = mysql_fetch_assoc($result)) 
    { 
    echo "<option>" .$row['BottleNumber']. "</option>"; 
    } 
  echo "</select>";
    } 
    else {
    echo "<option>No Customer Encoded</option></select>";  
    } 

    
echo "<font size = 2 >Type :</font>
<select name=\"bottletype[]\" onChange=\"this.name\"'>'; 
    <option selected>Select .. </option>
     <option> Slim </option>  
     <option> Round </option>  
</select><br>";
}

?>

<br>
  <input type="image" src="continue.png" width="100" height="25" name="login" border="0" />
</div> </form>

 

So what it basically do is..

User will input some data w/c i included as $_POST['customername'] and $_POST['quantity']

So when the user will input 5 quantity.. i want my loop to have 5 pairs of 2 drop down list.. well i suceeded there.

but, my problem is the 1st pair will output the  .$row['BottleNumber'].  w/c i included at the loop, and then the rest will have no output.. when i clicked them the dropdown list will only show the Select one. but for the 1st pair its working fine.

 

Sorry for the bad english.. someone help me pls :(

Link to comment
Share on other sites

What's with the onChange call?

 

Since I don't have a copy of your database, I can't execute your code to easily debug it. If you provided me with a stand-alone version of this script, I'd be happy to dig further.

 

Keep in mind, the two elements aren't necessarily tied together. You'd be better off using $i to accomplish this.

 

<select name="element[$i]">

Link to comment
Share on other sites

I think this is the problem sir. but not quite sure where.

 

for($i = 0; $i < $q; $i++){
    echo "Bottle Number :";

echo "<select name=\"bottlenum[]\" onChange=\"this.name\"> 
    <option>Select</option>";

    if(mysql_num_rows($result)) 
    { 
    while($row = mysql_fetch_assoc($result)) 
    { 
    echo "<option>" .$row['BottleNumber']. "</option>"; 
    } 
  echo "</select>";
    } 
    else {
    echo "<option>No Customer Encoded</option></select>";  
    } 

 

This code will suppose to dropdown the BottleNumbers in my database' well since i create 1 bottlenumber w/c is "1111"

the output of my dropdown would be "SELECT / 1111"

well it works fine but when the code looped and created 2 pairs of the dropdown.

the 2nd dropdown only output "SELECT".. :(

 

Help me please sir. :(

Link to comment
Share on other sites

When I run that code I get a FATAL ERROR.

 

When I fix that fatal error, I get an undefined variable error.

 

To help with the language barrier, please create a standalone script to replicate what you are trying to accomplish. Here's an example

 

<?php

$values_from_database = array( 'Option 1','Option 2','Option 3','Option 4','Option 5' );

// Check if quantity has been submitted, and it's numeric
if( isset($_POST['quantity']) && ctype_digit($_POST['quantity']) ) {
echo '<form method="post" action="">';
for( $i = 0; $i < $_POST['quantity'] && $i < 99; $i++ ) {
	echo '<select name="drop1['.$i.']">';
	foreach( $values_from_database as $v ) echo '<option>'.$v.'</option>';
	echo '</select><select name="drop2['.$i.']">';
	foreach( $values_from_database as $v ) echo '<option>'.$v.'</option>';
	echo '</select><br>';
}
echo '<input type="submit"></form>';

// Check if dropdowns have been submitted, and they're arrays
} elseif( isset($_POST['drop1']) && isset($_POST['drop2']) && is_array($_POST['drop1']) && is_array($_POST['drop2']) ) {

echo '<pre>';
print_r( $_POST['drop1'] );
print_r( $_POST['drop2'] );
echo '</pre>';

// Otherwise, post the default form
} else {
echo '<form method="post" action="">How many dropdowns?<input type="text" name="quantity"><input type="submit"></form>';
}

?>

Link to comment
Share on other sites

Hmm.. i dont know how can i do a standalone that can be the same output to what i want to do :(

but' here.. i just want to make a loop that outputs a dropdown list depending on how much quantity the user input..

 

Like if i put '2' as my quantity then it will output a 2 dropdown list that contains my values in my database.. can you help me make a code for that sir?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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