Jump to content

Variable question


TimUSA

Recommended Posts

How would I wrap this code up so that it was one variable?

 

echo'
<SELECT id="name" name="name[ ]" style="WIDTH: 160px" value ="';
if (isset($_POST['name[ ]'])) echo $_POST['name[ ]'];
echo '" />';

if(mysql_num_rows($result)) {
// we have at least one user, so show all users as options in select form
while($row=mysql_fetch_assoc($result)){
    echo '<option value="',$row['memberName'],'">',$row['memberName'],'</option>';
}
}
++$x;
}

Link to comment
https://forums.phpfreaks.com/topic/83418-variable-question/
Share on other sites

are you going to change this dynamically? 

 

if not, why not just make a function

call it stuff()

 

function stuff(){
echo'
<SELECT id="name" name="name[ ]" style="WIDTH: 160px" value ="';
if (isset($_POST['name[ ]'])) echo $_POST['name[ ]'];
echo '" />';

if(mysql_num_rows($result)) {
// we have at least one user, so show all users as options in select form
while($row=mysql_fetch_assoc($result)){
    echo '<option value="',$row['memberName'],'">',$row['memberName'],'</option>';
}
}
++$x;
}}

 

Then where you want that particular piece to go. call the function

 

<?php
stuff();
?>

Link to comment
https://forums.phpfreaks.com/topic/83418-variable-question/#findComment-424381
Share on other sites

this poses the same problem i have been having.

 

when ever i try to loop this code it returns blanks in the dropdown box.

leave it with out the loop and it works fine and returns the appropriate database values

 

here is the full code i am working with now:

$query = "SELECT `memberName` FROM `smf_members` WHERE `ID_GROUP` IN (1, 9, 10, 11, 13) ORDER BY `memberName`;";
$result = mysql_query($query);
$times = 13;
$x = 0;

function stuff() {
echo'
<SELECT id="name" name="name[ ]" style="WIDTH: 160px" value ="';
if (isset($_POST['name[ ]'])) echo $_POST['name[ ]'];
echo '" />';

if(mysql_num_rows($result)) {
// we have at least one user, so show all users as options in select form
while($row=mysql_fetch_assoc($result)){
    echo '<option value="',$row['memberName'],'">',$row['memberName'],'</option>';
}
}
++$x;
}

$x=0;
while($x<$times){
stuff();
++$x;
}

Link to comment
https://forums.phpfreaks.com/topic/83418-variable-question/#findComment-424385
Share on other sites

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.