Jump to content

Calling function though only getting one result back as the loop stops....


ndjustin20

Recommended Posts

Hello,

 

New to PHP and trying to loop through rows to and get the name of the "property" in this case.  I have tried foreach and while and neither seem to want to return all rows by calling the function.  Does it have something to do with echo'ing the table structure or can I not return an array out of a function like this?  I commented out the while loop as that wasn't working and put in a foreach loop though that didn't return all the data either.  Any help is greatly appreciated  :)

 

 

<?php

function getProperties(){

$sql = "SELECT propertyName FROM properties;";
$result = mysql_query($sql);
//$count = mysql_num_rows($result);
$property = mysql_fetch_array($result);
foreach($property as $propertyName){

echo  "<td width=\"157\" height=\"24\" valign=\"middle\"><div align=\"left\"><input type=\"checkbox\" name=\"$propertyName\"
\"id=\"$propertyName\" />$propertyName</div></td>";


}

/*
for($i=0;$i>=$count;$i++){

while($property = mysql_fetch_array($result)){

$propertyName = $property['propertyName'];

echo  "<td width=\"157\" height=\"24\" valign=\"middle\"><div align=\"left\"><input type=\"checkbox\" name=\"$propertyName\"
\"id=\"$propertyName\" />$propertyName</div></td>";

}
}
*/
}
?>

Link to comment
Share on other sites

Your method is only using the first returned result, by incorporating the $property=mysql_fetch_array() into the while statement, you can loop through each returned statement.

 

*Edit* I realized this may not work because of the quotes, so I changed it.

 

<?php

function getProperties(){

$sql = "SELECT propertyName FROM properties;";
$result = mysql_query($sql);
//$count = mysql_num_rows($result);
while($property = mysql_fetch_array($result){

echo  '<td width="157" height="24" valign="middle"><div align="left"><input type="checkbox" name="'.$property['propertyName'].'"
"id="'.$property['propertyName'].'" />'.$property['propertyName'].'</div></td>';


}?>

Link to comment
Share on other sites

The following code gave me the following error:

 

Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in E:\wamp\www\leaseCMS\includes\functions.php on line 10

 

Line 10 is marked in bold below :)

 

 

 


<?php

function getProperties(){
$sql = "SELECT propertyName FROM properties;";
$result = mysql_query($sql);
while($property = mysql_fetch_array($result)){

[b]echo  "<td width=\"157\" height=\"24\" valign=\"middle\"><div align=\"left\"><input type=\"checkbox\" [/b] name=\"$property['propertyName']\" \"id=\"$property['propertyName']\" />$property['propertyName']</div></td>"; 

}

}



?>


Link to comment
Share on other sites

Not a problem, glad to be of help. I thought I'd include a more detailed explanation incase you were interested.

 

-----

 

mysql_fetch_array() returns an array resource. So when you are selecting 'propertyName' from the database, mysql is returning something more like

array('propertyName'=>'value');

. We are then assigning that array to a variable called property. So

$property = array('propertyName'=>'value');

. To access propertyName we use

$property['propertyName']

 

By incoporating the assignment ($property = mysql_fetch_array()) into the while statement, we make PHP loop over each result that was returned and assign it one by one to $property.

 

 

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.