poleposters Posted April 19, 2008 Share Posted April 19, 2008 Hi, I'm having a problem with arrays. I'm only quite new to PHP. Can anyone show me what I'm doing wrong in the following code. I seem to be retrieving the same record for each array $row[slide][0],$row[slide][1],etc. $query = "SELECT slide FROM slideshow WHERE business_id=1"; $result = mysql_query ($query); $numlinks=mysql_num_rows($result); if($numlinks >0){ $row = mysql_fetch_assoc($result); $string = "<indexes>" . PHP_EOL; $string .= "<page" .PHP_EOL; $string .= "src=\"$row[slide][0]\">" . PHP_EOL; $string .= "</page>" . PHP_EOL; $string .= "<page" .PHP_EOL; $string .= "src=\"$row[slide][1]\">" . PHP_EOL; $string .= "</page>" . PHP_EOL; $string .= "<page" .PHP_EOL; $string .= "src=\"$row[slide][2]\">" . PHP_EOL; $string .= "</page>" . PHP_EOL; $string .= "</indexes>". PHP_EOL; } $open = fopen("file.xml", "w"); fwrite ($open, $string); fclose($open); Link to comment https://forums.phpfreaks.com/topic/101834-problem-with-arrays/ Share on other sites More sharing options...
miracle_potential Posted April 19, 2008 Share Posted April 19, 2008 Not quite sure, but I didnt notice you were missing a full stop before your first = on "$string =" $string .= $row[0] ..etc Link to comment https://forums.phpfreaks.com/topic/101834-problem-with-arrays/#findComment-521106 Share on other sites More sharing options...
Fadion Posted April 19, 2008 Share Posted April 19, 2008 Shouldnt those in-string arrays be like?: $string .= "src=\"$row['slide'][0]\">" Link to comment https://forums.phpfreaks.com/topic/101834-problem-with-arrays/#findComment-521118 Share on other sites More sharing options...
chigley Posted April 19, 2008 Share Posted April 19, 2008 <?php $query = "SELECT slide FROM slideshow WHERE business_id=1"; $result = mysql_query ($query); $numlinks=mysql_num_rows($result); if($numlinks >0){ $row = mysql_fetch_assoc($result); print_r($row); $string = "<indexes>" . PHP_EOL; $string .= "<page" .PHP_EOL; $string .= "src=\"$row[slide][0]\">" . PHP_EOL; $string .= "</page>" . PHP_EOL; $string .= "<page" .PHP_EOL; $string .= "src=\"$row[slide][1]\">" . PHP_EOL; $string .= "</page>" . PHP_EOL; $string .= "<page" .PHP_EOL; $string .= "src=\"$row[slide][2]\">" . PHP_EOL; $string .= "</page>" . PHP_EOL; $string .= "</indexes>". PHP_EOL; } $open = fopen("file.xml", "w"); fwrite ($open, $string); fclose($open); ?> Let us know the output of that. Link to comment https://forums.phpfreaks.com/topic/101834-problem-with-arrays/#findComment-521119 Share on other sites More sharing options...
Barand Posted April 19, 2008 Share Posted April 19, 2008 try <?php $query = "SELECT slide FROM slideshow WHERE business_id=1"; $result = mysql_query ($query); $numlinks=mysql_num_rows($result); if($numlinks >0){ $string = "<indexes>" . PHP_EOL; while ($row = mysql_fetch_assoc($result)) { // loop through returned rows $string .= "<page" .PHP_EOL; $string .= "src=\"$row['slide']\">" . PHP_EOL; $string .= "</page>" . PHP_EOL; } $string .= "</indexes>". PHP_EOL; } $open = fopen("file.xml", "w"); fwrite ($open, $string); fclose($open); ?> Link to comment https://forums.phpfreaks.com/topic/101834-problem-with-arrays/#findComment-521120 Share on other sites More sharing options...
poleposters Posted April 19, 2008 Author Share Posted April 19, 2008 Thank you. I solved it in the meantime. But both your suggestions are far more elgant than what I came up with. Still trying to get my head around arrays. Thank you for the insight. Link to comment https://forums.phpfreaks.com/topic/101834-problem-with-arrays/#findComment-521141 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.