PDA

View Full Version : dynamic dropdown wont duplicate


gavimobile
09-11-2008, 06:21 PM
hey folks, this is my dwc.php from the tutorial, im just trying to add a search form to it.
however if i type this code anywhere below the first one than nothing prints. if i put the line of code before the first one than it does print.


what im trying to print


<select name="lang" >
<?php
do {
?>
<option value="<?php echo $row_dyn['dyn_abv']; ?>"><?php echo $row_dyn['language']; ?></option>
<?php
}
while ($row_dyn = mysql_fetch_array($results_dyn));
?>
</select>

again: in my insert form on the upper part of the page, this code displays fine, but if i put this code anywhere below the first of the same exact code towards the upper part of the page than it wont print. here is my full code.



<?php
require_once('../includes/config.inc.php');

// query for stories
$query = sprintf("SELECT * FROM stories WHERE story_id='$upd'");
$results = mysql_query($query);
$row_upd = mysql_fetch_array($results);

// variables must be filled
if ($submit && $title && $desc && ! $update_id){

// Insert Query
mysql_query("INSERT INTO stories (story_lang, story_title, story_desc, story_date, story_num) VALUES ('$lang', '$title', '$desc', '$date', '$num')");

}elseif($submit && $update_id){

mysql_query("UPDATE stories set story_lang='$lang', story_title='$title', story_desc='$desc', story_num='$num' WHERE story_id = '$update_id' ");
}

if ($del){

// Delete Query
mysql_query("DELETE FROM stories WHERE story_id='$del'");
}


// query for stories
$query = sprintf("SELECT * FROM stories");
$results = mysql_query($query);
$row = mysql_fetch_array($results);

// query for dynamic drop down
$query_dyn = sprintf("SELECT * FROM dyanmic_lang");
$results_dyn = mysql_query($query_dyn);
$row_dyn = mysql_fetch_array($results_dyn);

// menu selected
$add_stories = "id='current'";
?>

<html>
<head>
<title>Add Stories - Content Manager</title>
</head>
<body>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<p>
<label>Language:</label>
<select name="lang" >
<?php
do {
?>
<option value="<?php echo $row_dyn['dyn_abv']; ?>"><?php echo $row_dyn['language']; ?></option>
<?php
}
while ($row_dyn = mysql_fetch_array($results_dyn));
?>
</select>

<label>Fund #:</label>
<input type="text" name="num" value="<?php echo $row_upd['story_num']; ?>" size="4" />

<label>Title:</label>
<input name="title" type="text" size="30" value="<?php echo $row_upd['story_title']; ?>" />

<label>Description</label>
<textarea name="desc" rows="5" cols="5"><?php echo $row_upd['story_desc']; ?></textarea>
<label>Date:</label>
<?php echo date('Y-m-d'); ?>
<br /><br />
<input type="hidden" name="date" value="<?php echo date('Y-m-d'); ?>" />
<input type="hidden" name="update_id" value="<?php echo $row_upd['story_id']; ?>" />
<input type="hidden" name="submit" value="1" />
<input class="button" type="submit" value="Save Story" />
</p>
</form>

<!-- here is a search form im in the process of making -->
<form name="form2" action="<?php echo $_SERVER['PHP_SELF']; ?>" >
<select name="lang" >
<?php
do {
?>
<option value="<?php echo $row_dyn['dyn_abv']; ?>"><?php echo $row_dyn['language']; ?></option>
<?php
}
while ($row_dyn = mysql_fetch_array($results_dyn));
?>
</select>
<input class="button" type="submit" value="Search" />
</form>

<!-- here i have results -->

</body>
</html>

edbr
09-12-2008, 02:28 AM
i think you need the select query added to the form just before the do{

gavimobile
09-12-2008, 04:03 AM
i think you need the select query added to the form just before the do{

hey edbr, if you ment i need to add the select query 2 times, 1 before each dynamic drop down, than you are correct! it worked!

but can you explain to me why it needs to be added 2 times?

thanks
gavimobile

edbr
09-12-2008, 04:51 AM
Not really confident here bur where you have after the }while blah blah of the first query it negates/confuses the second query.
i just found from trial and error to keep them seperate.

lux
09-12-2008, 11:14 AM
When you query the database you are creating a result of rows (if more than one row is returned in the query)

when you loop through the result you are looping through the rows

at the end of the loop you are at the end of the rows returned in the database query

to loop through the rows again you need to start at the first row again

you can use the mysql_data_seek() function to return to a specified row in your result

in this example i have specified the row to return to as 0 as i want to start from the first row (remember we are dealing with arrays and arrays start at 0)


$query = mysql_query("SELECT * FROM dyanmic_lang");


while($result = mysql_fetch_assoc($query)){
echo $result['dyn_abv'];
}

mysql_data_seek($query, 0);

while($result = mysql_fetch_assoc($query)){
echo $result['dyn_abv'];
}


hope this helps

let me know if this isn't clear

gavimobile
09-12-2008, 02:10 PM
When you query the database you are creating a result of rows (if more than one row is returned in the query)

when you loop through the result you are looping through the rows

at the end of the loop you are at the end of the rows returned in the database query

to loop through the rows again you need to start at the first row again

you can use the mysql_data_seek() function to return to a specified row in your result

in this example i have specified the row to return to as 0 as i want to start from the first row (remember we are dealing with arrays and arrays start at 0)


$query = mysql_query("SELECT * FROM dyanmic_lang");


while($result = mysql_fetch_assoc($query)){
echo $result['dyn_abv'];
}

mysql_data_seek($query, 0);

while($result = mysql_fetch_assoc($query)){
echo $result['dyn_abv'];
}
hope this helps

let me know if this isn't clear

i dont think im quite ready for this, but if mysql_data_seek returns specified rows, than i dont see how this can be used in my script. I remember arrays start at 0, but i thought arrays are like this array(the, quick, fox, jumped, over, the, fence)

thanks buddy
gavimobile

lux
09-12-2008, 02:51 PM
mysql_data_seek doesn't return specific rows it moves the internal pointer to a specified row. i think this is the bit it don't explain well.

say for example you query your database (mysql_query() )and the query returns 5 rows and you store them in an array (mysql_fetch_array() ) well when you loop through the array (while loop / for loop) php needs to know which row it is pointing at, this is the internal pointer. when the loop has finished the internal pointer remains at the last row so if you try to loop again your starting at the last row.

using mysql_data_seek you can set the internal pointer to a specific row of the database query result. and your example you can specify 0 so that you start at the first row.

stick the code I pasted in your page and see the effect of changing the 0 to 1,2,3 etc.

hope this helps

gavimobile
09-15-2008, 12:38 PM
mysql_data_seek doesn't return specific rows it moves the internal pointer to a specified row. i think this is the bit it don't explain well.

say for example you query your database (mysql_query() )and the query returns 5 rows and you store them in an array (mysql_fetch_array() ) well when you loop through the array (while loop / for loop) php needs to know which row it is pointing at, this is the internal pointer. when the loop has finished the internal pointer remains at the last row so if you try to loop again your starting at the last row.

using mysql_data_seek you can set the internal pointer to a specific row of the database query result. and your example you can specify 0 so that you start at the first row.

stick the code I pasted in your page and see the effect of changing the 0 to 1,2,3 etc.

hope this helps

lux,

it worked like gold!!!!! much cleaner than pasteing the query 2 times! i like it!

thanks buddy,

MARK AS SOLVED