Forum Moderators: coopster

Message Too Old, No Replies

Repeat a php populated listbox on a page

         

rnconync

5:11 pm on Apr 17, 2008 (gmt 0)

10+ Year Member



here is my code and what I would like to do is replace the change vendor column php loop with, if possible, a php variable that refers to the loop. Whats happening is that I might have 300 records that need to be displayed with the change vendor/select a new vendor selection box next to each record for updating if needed....and this happens with the way the code is now, but it is incredible slow because it is "redrawing and repopulating" the listbox for every record that is listed.

Is there an easier way to have the listbox populated once, assign a variable to it (or something) and then just "refer" to it multiple times for each recorde displayed so that the box is not repeating the code for every record displayed?

Thank in advance for any help, it is really appreciated!


<table border="0">
<tr>
<td width="125"><strong>Cost Code</strong></td>
<td width="125"><strong>Vendor Assigned</strong></td>
<td><strong>Change Vendor</strong></td>
</tr>
<?php do { ?>
<tr id="assigned-<?php echo $row_vendcchood['id']; ?>">
<td width="125"><?php echo $row_vendcchood['costcode']; ?></td>
<td width="125"><?php echo $row_vendcchood['vendor']; ?></td>
<td>
<select name="change[]" id="change">
<option value="0">select a new Vendor</option>
<?php do { ?>
<option value="<?php echo $row_vendors['id']?>"><?php echo $row_vendors['vendor']?></option>
<?php }
while ($row_vendors = mysql_fetch_assoc($vendors));
$rows = mysql_num_rows($vendors);
if($rows > 0) {
mysql_data_seek($vendors, 0);
$row_vendors = mysql_fetch_assoc($vendors);}?>
</select></td>
<td>
<select name="delete[]" id="delete">
<option value="0">Select to Delete</option>
<option value="1">Delete Entry</option>
</select><input name="id[]" type="hidden" value="<?php echo $row_vendcchood['id']; ?>" />
</td>
</tr>
<?php } while ($row_vendcchood = mysql_fetch_assoc($vendcchood)); ?> <tr>
<td colspan="3" align="center">
<input type="submit" name="Submit" id="Submit" value="Submit" /></td>
</tr>
</table>

jatar_k

5:23 pm on Apr 17, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld rnconync,

you want to have the loop build the html into a var instead of just echoing it

can you isolate the loop you would like to repeat for me?

rnconync

6:31 pm on Apr 17, 2008 (gmt 0)

10+ Year Member



This is the specific loop that I am trying to repeat for every record (without running the loop for each record). It is what populates this select box.
<select name="change[]" id="change">

<?php do { ?>
<option value="<?php echo $row_vendors['id']?>"><?php echo $row_vendors['vendor']?></option>
<?php }
while ($row_vendors = mysql_fetch_assoc($vendors));
$rows = mysql_num_rows($vendors);
if($rows > 0) {
mysql_data_seek($vendors, 0);
$row_vendors = mysql_fetch_assoc($vendors);}?>

could you eplain or provide a short example pf what you are referring to? I am somewhat new to most of this still.

Thanks again for your help!

rnconync

1:51 pm on Apr 18, 2008 (gmt 0)

10+ Year Member



Any other input? Thanks!

jatar_k

1:56 pm on Apr 18, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



had this open on another tab but hadn't had time yet to answer

so what you need to do is append all the html to a variable instead of sending it to the browser.

something like this

$thedrop = '';
$thedrop .= '<select name="whatever">';
while ($row_vendors = mysql_fetch_assoc($vendors)) {
$thedrop .= '<option value="' . $row_vendors['id'] . '">' . $row_vendors['vendor'] . '</option>';
}
$thedrop .= '</select>';

then you can just

echo $thedrop;

to output the constructed code

rnconync

2:06 pm on Apr 18, 2008 (gmt 0)

10+ Year Member



Wow! thanks jatar! That was pretty simple and very much appreciated...after you posted that I had a flashback to where I am doing something very similar to insert multiple records into a database. I just forgot all about it!

for( $i = 0, $c = count($vendors); $i < $c; $i += 1 ){

$query .= " (".$vendors[$i].",".$ccodes[$i].",".$primary_key."),";}

$query = substr_replace($query,"",-1);
$mysql_return = mysql_query("INSERT INTO vendcchood (vendor, costcode, nhood) VALUES".$query) or die(mysql_error());

Thanks so much again, what a great place! I have been scratching my head to get that little bit figured out for a while now...

jatar_k

2:08 pm on Apr 18, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



glad to help

rnconync

3:31 pm on Apr 18, 2008 (gmt 0)

10+ Year Member



this is actually much quicker, but when I am getting more records (say 100) it takes about 10 seconds to load the info still. Is there a way to make the whole select box a "drop-in" element?

jatar_k

3:59 pm on Apr 18, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



how often does the data change?

you could generate it when a new vendor is added and then just include a static drop down

rnconync

4:22 pm on Apr 18, 2008 (gmt 0)

10+ Year Member



the data changes somewhat often and it would be best if it could pull from the database.....This is my first choice because it requires no interaction when data is changed.

If I wanted to, whats the best way to generate (export) the options from a the database to a static list.

jatar_k

4:33 pm on Apr 18, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



>> changes somewhat often

if that is less than every day then you could have a cron that ran every night that created that same html, saved it to a variable and then wrote it to a file. You could then include that file wherever you needed it.

you might want to have it as a function and pass it a name for the select

rnconync

4:38 pm on Apr 18, 2008 (gmt 0)

10+ Year Member



hmmm...it is less than every day. I'm not following on your last comment:
>>you might want to have it as a function and pass it a name for the select

jatar_k

5:28 pm on Apr 18, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I am not sure if every time you insert a select element if you want it to have the same name

if you use it twice in one form you would need it to have a different name each time

rnconync

7:14 pm on Apr 18, 2008 (gmt 0)

10+ Year Member



Ah, no it is fine because the select box is a php array
<select name="change[]" id="change">