0

i am trying to create xml sitemap i have a column album_image in my database. in start day's i did't upload album image and the column store '0' as value after years ago i start uploading album image. now i want to create sitemap for it. if album_image == 0 display static image else display album iamge. how can i use if statment in $xml .=

$xml .= '<image:image>';
        $xml .= '<image:loc>'.SITE_URL. if($row->album_image == '0'){'images/video_icon.png'}else{'media/'.$row->cat_id.'/images/'.$row->album_image.'}</image:loc>';
        $xml .= '<image:caption>'.$row->album_name.'</image:caption>';
        $xml .= '</image:image>';

i am getting this error " Parse error: syntax error, unexpected T_IF in E:\wamp\www\site\sitemap-album.php on line 15 "

Arman
  • 55
  • 6

4 Answers4

1

You need to separate the if condition:

$xml .= '<image:image>';
$xml .= '<image:loc>'.SITE_URL;

if($row->album_image == '0') {
   $xml .= 'images/video_icon.png';
 } else {
   $xml .= 'media/'.$row->cat_id.'/images/'.$row->album_image;
 }

 $xml .= '</image:loc>';
 $xml .= '<image:caption>'.$row->album_name.'</image:caption>';
 $xml .= '</image:image>';
Farkie
  • 3,307
  • 2
  • 22
  • 33
0

you can user this also $xml .= '';

 $xml .= '<image:loc>'.SITE_URL. ($row->album_image == '0')?'images/video_icon.png':'media/'.$row->cat_id.'/images/'.$row->album_image.'</image:loc>';
    $xml .= '<image:caption>'.$row->album_name.'</image:caption>';
    $xml .= '</image:image>';
sunil
  • 99
  • 5
0

the full code is bellow . now i m geting "Parse error: syntax error, unexpected '=' in"

$sql = "SELECT * from albums  LIMIT 0, 1";
$query = mysql_query($sql);
if(mysql_num_rows($query) > 0){
    while($row = mysql_fetch_object($query)){
        $xml .= '<url>';
        $xml .= '<loc>'.SITE_URL.$row->album_url.'-album-1</loc>';
        $xml .= '</url>';
        $xml .= '<image:image>';
        $xml .= '<image:loc>'.SITE_URL;

        if($row->album_image == '0') {
           $xml . = 'images/video_icon.png';
         } else {
           $xml .= 'media/'.$row->cat_id.'/images/'.$row->album_image.';
         }

         $xml .= '</image:loc>';
         $xml .= '<image:caption>'.$row->album_name.'</image:caption>';
         $xml .= '</image:image>';
    }
}
$xml .= '</urlset>';
echo $xml;
Arman
  • 55
  • 6
0

Every PHP statement must end with semi colon (;).

It has an exception: Last statement in the file.

echo statement after some code needs that previous statement is closed.

Concatenation can work, but, for echo, closing previous statement is the only option.

Use ternary operator and store result in variable.

Use that variable to make code readable.

Complete code:

$videoImage = ($row->album_image == '0') ? 'images/video_icon.png' : 'media/'.$row->cat_id.'/images/'.$row->album_image;
$xml .= '<image:image>';
$xml .= '<image:loc>'.SITE_URL . $videoImage . '</image:loc>';
$xml .= '<image:caption>'.$row->album_name.'</image:caption>';
$xml .= '</image:image>';
Pupil
  • 23,834
  • 6
  • 44
  • 66