ndjustin20 Posted August 9, 2010 Share Posted August 9, 2010 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>"; } } */ } ?> Quote Link to comment Share on other sites More sharing options...
xangelo Posted August 9, 2010 Share Posted August 9, 2010 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>'; }?> Quote Link to comment Share on other sites More sharing options...
onlyican Posted August 9, 2010 Share Posted August 9, 2010 $propertyName is an array, Within your foreach loop try adding this echo'<pre>';print_r($propertyName);echo'</pre>'; See your results, tell us your results Quote Link to comment Share on other sites More sharing options...
ndjustin20 Posted August 9, 2010 Author Share Posted August 9, 2010 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>"; } } ?> Quote Link to comment Share on other sites More sharing options...
xangelo Posted August 9, 2010 Share Posted August 9, 2010 I thought as much. I corrected my post to include a fix for the new single/double quote issue which is what is throwing the error. Quote Link to comment Share on other sites More sharing options...
onlyican Posted August 9, 2010 Share Posted August 9, 2010 Try removing the [ b ] before the echo Also property['propertyName']. where 'propertyName' is the Field from the Database. Quote Link to comment Share on other sites More sharing options...
ndjustin20 Posted August 9, 2010 Author Share Posted August 9, 2010 Ok so for some reason I am only able to get the first row returned when i used echo'<pre>';print_r($propertyName);echo'</pre>'; to check to see what is actually being pulled. Quote Link to comment Share on other sites More sharing options...
ndjustin20 Posted August 9, 2010 Author Share Posted August 9, 2010 The I was thinking would bold that row so you knew which one was row 10 sorry any other suggestions to get it going? Quote Link to comment Share on other sites More sharing options...
onlyican Posted August 9, 2010 Share Posted August 9, 2010 also you have [ / b ] within the string (After checkbox) And if you want to change your code and try a previous example, it wont work <pre>';print_r($property);echo'</pre>'; Quote Link to comment Share on other sites More sharing options...
xangelo Posted August 9, 2010 Share Posted August 9, 2010 In the loop do this: echo '<pre>'.print_r($property,true).'</pre>'; propertName is nothing but a key in the array, so you will get nothing outputted. The actual variable is called $property Quote Link to comment Share on other sites More sharing options...
ndjustin20 Posted August 9, 2010 Author Share Posted August 9, 2010 Ok so you fixed it using '.$property['propertyName'].' so each time I want to use a key=>value pairing inside a function I should us that format I would assume as that did the trick right away :) :) Quote Link to comment Share on other sites More sharing options...
ndjustin20 Posted August 9, 2010 Author Share Posted August 9, 2010 Thank you very much for your help!!!!!!!!!!!!! :) :) Quote Link to comment Share on other sites More sharing options...
xangelo Posted August 9, 2010 Share Posted August 9, 2010 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.