lorddemos90 Posted December 12, 2006 Share Posted December 12, 2006 Here is the code for the value "str"$str = "<?xml version='1.0'?><properties><?php do {<property><city>$city</city></property>}while ($row_rscontacts = mysql_fetch_assoc($rscontacts));?></properties>";What I want to do is loop everything within the <property> tags for each table row in the mysql database. I tried inserting a loop as you can see, but it's just taking my code as literal text and not actually processing it. So how is this done? I'm a beginning programmer so please make sure to explain all code that is typed. Link to comment https://forums.phpfreaks.com/topic/30342-how-do-i-loop-part-of-a-value/ Share on other sites More sharing options...
boby Posted December 12, 2006 Share Posted December 12, 2006 You could use this code:[code=PHP]<?php$str = "<?xml version='1.0'?>\n<properties>\n";while ($row_rscontacts = mysql_fetch_assoc ($rscontacts)){ $str .= "\t<property>\n \t\t<city>{$city}</city>\n \t</property>\n";}$str .= "</properties>";?>[/code][b]Note:[/b] There is a "." (dot) in the while loop and at the last [b]$str[/b] before the "=" sign, this means the values are appended to the current string and the value is not overwritten.$str [b][color=red].[/color][/b]=I've also changed the loop a bit, added the while clause on top to make sure it writes something only if you have some values.Boby Link to comment https://forums.phpfreaks.com/topic/30342-how-do-i-loop-part-of-a-value/#findComment-139594 Share on other sites More sharing options...
lorddemos90 Posted December 12, 2006 Author Share Posted December 12, 2006 I'm getting "Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in C:\htdocs\databasestuff\testxml.php on line 44"line 44 is while ($row_rscontacts = mysql_fetch_assoc ($rscontacts)) Link to comment https://forums.phpfreaks.com/topic/30342-how-do-i-loop-part-of-a-value/#findComment-139605 Share on other sites More sharing options...
boby Posted December 12, 2006 Share Posted December 12, 2006 Here is a more detailed example, make sure the SQL query is correct and I've also noticed you are using [b]$city[/b] but the variable was not assigned. Use [b]$row_rscontacts['db_table_field'][/b], in the following example it's [b]$row_rscontacts['city'][/b].[code=PHP]<?php//Build SQL query$sql = "SELECT `city` FROM `db_users`";//Run SQL query$rscontacts = mysql_query ($sql);//Check if query was successfullif (!$rscontacts){ echo "Could not successfully run query ({$sql}) from DB: " . mysql_error(); exit;}$str = "<?xml version='1.0'?>\n<properties>\n";//Check if we have resultsif (mysql_num_rows ($rscontacts) > 0){ //Loop through each found row while ($row_rscontacts = mysql_fetch_assoc ($rscontacts)) { $str .= "\t<property>\n \t\t<city>".$row_rscontacts['city']."</city>\n \t</property>\n"; }}$str .= "</properties>";?>[/code] Link to comment https://forums.phpfreaks.com/topic/30342-how-do-i-loop-part-of-a-value/#findComment-139622 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.