Page 1 of 1

[SOLVED]-else statement.

Posted: Fri Jan 30, 2004 2:11 pm
by ol4pr0
Oke.. some of you might have seen this from database.. however its no longer a database question i guess.

i made some changes in the script

Code: Select all

while ($row = mysql_fetch_array($result))

// before this was if ($row = mysql_fetch_array($result))
// which cuases it not to fetch all rows
which gave me this error.
Parse error: parse error, unexpected T_ELSE in xml.php on line 46

So now i am stuck on how to change the Else statement in line 46

compleet script

Code: Select all

$result = mysql_query($query) or die (mysql_error()); 
$num = mysql_num_rows($result); 

if ($num != 0) 
   { 
   $file= fopen("resulta.xml", "w"); 
   $_xml ="<?xml version="1.0" encoding="UTF-8"?>\r\n"; 
   $_xml .="<Caudat_xml>\r\n"; 

   while ($rows = mysql_fetch_array($result)) 
      {
      $_xml .="\t<num_paquete>="" . $rows["Paqnr"] . "">\r\n";$_xml .="\t</num_paquete>\r\n"; 
      $_xml .="\t<empresa>="" . $rows["Empresa"] . "">\r\n";$_xml .="\t</empresa>\r\n"; 
      $_xml .="\t<codigo>="" . $rows["Codigo"] . "">\r\n";$_xml .="\t</codigo>\r\n"; 
      $_xml .="\t<envio>="" . $rows["Envio"] . "">\r\n";$_xml .="\t</envio>\r\n"; 
      $_xml .="\t<desde>="" . $rows["Desde"] . "">\r\n";$_xml .="\t</desde>\r\n"; 
      $_xml .="\t<peso_total>="" . $rows["Peso_Total"] . "">\r\n";$_xml .="\t</peso_total>\r\n"; 
      $_xml .="\t<peso>="" . $rows["Peso"] . "">\r\n";$_xml .="\t</peso>\r\n"; 
      $_xml .="\t<valor>="" . $rows["Valor"] . "">\r\n";$_xml .="\t</valor>\r\n"; 
      $_xml .="\t<codigo_env>="" . $rows["Codigo_Env"] . "">\r\n";$_xml .="\t</codigo_env>\r\n"; 
      $_xml .="\t<informacion>="" . $rows["Information"] . "">\r\n";$_xml .="\t</informacion>\r\n"; 
      //$_xml .="\t<dato="" .$hoy.  "">\r\n";$_xml .="\t</dato>\r\n"; 
      } 
      else //------------->  line  46
         { 
         $_xml .="\t<Query_Empty>="No Resulta">\r\n"; 
         $_xml .="\t</Query_Empty>\r\n"; 
         } 
         } 
         $_xml .="</Caudat_xml>"; 
         fwrite($file, $_xml); 
         fclose($file); 
         echo "XML formato list.  <a href="resulta.xml">Por Aqui el formato XML.</a>"; 

?>
How should i do this? any suggestions![/i]

Posted: Fri Jan 30, 2004 2:13 pm
by markl999
if(mysql_num_rows($result) > 0){
while($rows = mysql_fetch_array($result)){
.....
}
} else {
//there were no rows returned
}

Posted: Fri Jan 30, 2004 2:21 pm
by ol4pr0
I wished that did the trick ;-)

unless i did some wrong

Code: Select all

<?
$result = mysql_query($query) or die (mysql_error()); 
$num = mysql_num_rows($result); 

$file= fopen("resulta.xml", "w"); 
$_xml ="<?xml version="1.0" encoding="UTF-8"?>\r\n"; 
$_xml .="<Caudat_xml>\r\n"; 
if(mysql_num_rows($result) > 0){ 
while($rows = mysql_fetch_array($result))
	{?>

Posted: Fri Jan 30, 2004 2:23 pm
by Unipus
Yeah, you didn't close your original IF statement.

Posted: Fri Jan 30, 2004 2:23 pm
by markl999
Hard to tell from that snippet, need to see more ;)
And if it doesn't work, maybe state what's happening, any errors etc...

Posted: Fri Jan 30, 2004 2:29 pm
by ol4pr0
Error ocured line 47 now.

Code: Select all

$file= fopen("resulta.xml", "w"); 
$_xml ="<?xml version="1.0" encoding="UTF-8"?>\r\n"; 
$_xml .="<Caudat_xml>\r\n"; 
if(mysql_num_rows($result) > 0){ 
while($rows = mysql_fetch_array($result)) 
   {
      $_xml .="\t<num_paquete>="" . $rows["Paqnr"] . "">\r\n";$_xml .="\t</num_paquete>\r\n"; 
      $_xml .="\t<empresa>="" . $rows["Empresa"] . "">\r\n";$_xml .="\t</empresa>\r\n"; 
      $_xml .="\t<codigo>="" . $rows["Codigo"] . "">\r\n";$_xml .="\t</codigo>\r\n"; 
      $_xml .="\t<envio>="" . $rows["Envio"] . "">\r\n";$_xml .="\t</envio>\r\n"; 
      $_xml .="\t<desde>="" . $rows["Desde"] . "">\r\n";$_xml .="\t</desde>\r\n"; 
      $_xml .="\t<peso_total>="" . $rows["Peso_Total"] . "">\r\n";$_xml .="\t</peso_total>\r\n"; 
      $_xml .="\t<peso>="" . $rows["Peso"] . "">\r\n";$_xml .="\t</peso>\r\n"; 
      $_xml .="\t<valor>="" . $rows["Valor"] . "">\r\n";$_xml .="\t</valor>\r\n"; 
      $_xml .="\t<codigo_env>="" . $rows["Codigo_Env"] . "">\r\n";$_xml .="\t</codigo_env>\r\n"; 
      $_xml .="\t<informacion>="" . $rows["Information"] . "">\r\n";$_xml .="\t</informacion>\r\n"; 
      //$_xml .="\t<dato="" .$hoy.  "">\r\n";$_xml .="\t</dato>\r\n"; 
      } 
      else 
         {  // ---> line 47
         $_xml .="\t<Query_Empty>="No Resulta">\r\n"; 
         $_xml .="\t</Query_Empty>\r\n"; 
         } 
         } 
         $_xml .="</Caudat_xml>"; 
         fwrite($file, $_xml); 
         fclose($file); 
         echo "XML formato list.  <a href="resulta.xml">Por Aqui el formato XML.</a>";

Posted: Fri Jan 30, 2004 2:31 pm
by markl999
Your doing
if(){
while(){
} else {
}
}

you need to be doing
if(){
while(){
}
} else {
}

Posted: Fri Jan 30, 2004 2:33 pm
by ol4pr0
Unipus wrote:Yeah, you didn't close your original IF statement.

Code: Select all

} // by adding this.. i needed to delete the line 53 }
} else {
you were right about that one..

Close it one time to many :oops:

Code: Select all

else 
         { 
         $_xml .="\t<Query_Empty>="No Resulta">\r\n"; 
         $_xml .="\t</Query_Empty>\r\n"; 
         } 
         // { line 53
         $_xml .="</Caudat_xml>";

Now it does work ;-)