Jump to content

if - elseif-else not work


Nappi

Recommended Posts

Hi all

 

I have a problem with this code

 

$mylines = new xml_line("xmlfile.xml", "hixml");
$mylines->get_record(0,"member","");
$mylines->xml_stream();

for ($i = 0; $i <= 500; $i++) {

$name = $mylines->table_result[0][$i]['nickname'];
$rang = $mylines->table_result[0][$i]['rank_name'];
$prof = $mylines->table_result[0][$i]['profession'];
$lvl = $mylines->table_result[0][$i]['level'];
 $res = $mylines->table_result[0][$i]['photo_url'];
$image = substr($res, 47);

if ($rang == 'President' or 'General') {

$file = one.txt';
$filename = './templates/one.html';

} elseif ($rang == 'Squad Commander') {

$file = 'two.txt';
$filename = './templates/two.html'; 

} else {

$file = 'three.txt';
$filename = './templates/three.html'; 

}

 

The variable $file and $filename is not set like i will, all entris are in the first files "one.txt and one.html" waths wrong ?

 

Selma

Link to comment
https://forums.phpfreaks.com/topic/205780-if-elseif-else-not-work/
Share on other sites

This is not how you write an "if" statement with an "or"

<?php
if ($rang == 'President' or 'General') {
?>

use

<?php
if ($rang == 'President' || $rang == 'General') {
?>

 

You're missing an initial single quote here:

<?php
$file = one.txt';
?>

use

<?php
$file = 'one.txt';
?>

 

Ken

 

Okay I'll try to explain

 

after the loop is the only variable in what condition are as specified for the loop is. Only the entries by Squad Commander are made so by Squad Commander

 

At the moment he write everything into the last, tree.html, file. without having made a assignment.

 

I've also have treated each section for themselves.

 

if ($rang == 'President' || $rang == 'General') {
    $file1 = './templates/one.html';  
//sicher gehen, dass die Datei existiert und beschreibbar ist
if (is_writable($file1)) {

     // Wir ffnen $filename im "Anhnge" - Modus.
     // Der Dateizeiger befindet sich am Ende der Datei, und
     // dort wird $somecontent spter mit fwrite() geschrieben.
     if (!$handle = fopen($file1, "w")) {
          exit;
     }

     // Schreibe $somecontent in die geffnete Datei.
     if (!fwrite($handle, $content)) {
         exit;
     }


     fclose($handle);


  }
}

 

for the second

 

if  ($rang == 'Squad Commander' && ($rang != 'President' || $rang != 'General')) {
     $file2= './templates/two.html';  
//sicher gehen, dass die Datei existiert und beschreibbar ist
if (is_writable($file2)) {

     // Wir ffnen $filename im "Anhnge" - Modus.
     // Der Dateizeiger befindet sich am Ende der Datei, und
     // dort wird $somecontent spter mit fwrite() geschrieben.
     if (!$handle = fopen($file2, "w")) {
          exit;
     }

     // Schreibe $somecontent in die geffnete Datei.
     if (!fwrite($handle, $content)) {
         exit;
     }


     fclose($handle);


  }
}

 

If I do so, I write everything from the first row to the last line of the entry in my file.

 

But I would like only the messages of - such as "squad commander" - have, everything before and after should not be written to the file.

 

I hope you can help me

 

greetings Selma

 

 

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.