Jump to content

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

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.