Jump to content

Problem with arrays.


poleposters

Recommended Posts

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

<?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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.