|
Categorieën >
PHP & SQL
php > xml (Opgelost)
TekRequest – 09/06/2009 16:05 (Laatst gewijzigd op 09/06/2009 16:07)
|
|
offline
|
Nieuw lid
|
Hello,
ik probeer via sql > php > xml out te putten, dat lukt me aardig toch 1 probleempje
Mijn tabel ziet er zo uit :
id pad titel width categorie_id
23 Image_9391_LR.jpg 0 2
24 Image_9399_LR.jpg 0 5
25 Image_9406_LR.jpg 0 5
26 Image_9421_LR.jpg 0 5
Er moet gestart worden met het openen van een categorie
Zolang mijn categorie_id hetzelfde blijft mag enkel dit regeltje toegevoegd worden :
$xml_output .= "\t\t<foto pad='$pad' titel='$titel' width='$width' />\n";
$xml_output .= "\t\t<foto pad='$pad' titel='$titel' width='$width' />\n";
anders moet een node <category> aangemaakt worden met </category>
<?php
header("Content-type: text/xml");
$host = "";
$user = "";
$pass = "";
$database = "";
$linkID = mysql_connect($host, $user, $pass) or die("Could not connect to host.");
mysql_select_db($database, $linkID) or die("Could not find database.");
$query = "SELECT * FROM categorie INNER JOIN foto ON categorie.id = foto.categorie_id ORDER BY foto.categorie_id ASC";
$resultID = mysql_query($query, $linkID) or die("Data not found.");
$xml_output = "<?xml version=\"1.0\"?>\n";
$xml_output .= "<gallery>\n";
$vorige_categorie = 0;
$huidige_categorie = 0;
for($i = 0 ; $i < mysql_num_rows($resultID) ; $i++)
{
$row = mysql_fetch_assoc($resultID);
$pad = $row['pad'];
$titel = $row['titel'];
$width = $row['width'];
$name = $row['naam'];
$huidige_categorie = $row['categorie_id'];
// echo $categorie_id;
// ALS CATEGORIE VERANDERT , NIEUWE CATEGORIE OPSTARTEN
if ($huidige_categorie == $vorige_categorie) {
$xml_output .= "\t\t<foto pad='$pad' titel='$titel' width='$width' />\n";
// Escaping illegal characters
$row['titel'] = str_replace("&", "&", $row['titel']);
$row['titel'] = str_replace("<", "<", $row['titel']);
$row['titel'] = str_replace(">", ">", $row['titel']);
$row['titel'] = str_replace("\"", """, $row['titel']);
} else if ($huidige_categorie != $vorige_categorie) {
$xml_output .= "\t<category name='$name'>\n";
$xml_output .= "\t\t<foto pad='$pad' titel='$titel' width='$width' />\n";
// Escaping illegal characters
$row['titel'] = str_replace("&", "&", $row['titel']);
$row['titel'] = str_replace("<", "<", $row['titel']);
$row['titel'] = str_replace(">", ">", $row['titel']);
$row['titel'] = str_replace("\"", """, $row['titel']);
$xml_output .= "\t</category>\n";
}
$vorige_categorie = $huidige_categorie;
}
$xml_output .= "</gallery>";
echo $xml_output;
/*
$file= fopen("myFile.xml", "w");
fwrite($file, $xml_output);
fclose($file);
print "<meta http-equiv=\"refresh\"content=\"0;URL=update_ok.php\">";
exit();
*/
?>
<?php header("Content-type: text/xml"); $host = ""; $user = ""; $pass = ""; $database = ""; $linkID = mysql_connect($host, $user, $pass) or die("Could not connect to host."); $query = "SELECT * FROM categorie INNER JOIN foto ON categorie.id = foto.categorie_id ORDER BY foto.categorie_id ASC"; $xml_output = "<?xml version=\"1.0\"?>\n"; $xml_output .= "<gallery>\n"; $vorige_categorie = 0; $huidige_categorie = 0; { $pad = $row['pad']; $titel = $row['titel']; $width = $row['width']; $name = $row['naam']; $huidige_categorie = $row['categorie_id']; // echo $categorie_id; // ALS CATEGORIE VERANDERT , NIEUWE CATEGORIE OPSTARTEN if ($huidige_categorie == $vorige_categorie) { $xml_output .= "\t\t<foto pad='$pad' titel='$titel' width='$width' />\n"; // Escaping illegal characters $row['titel'] = str_replace(">", ">", $row['titel']); $row['titel'] = str_replace("\"", """, $row['titel']); } else if ($huidige_categorie != $vorige_categorie) { $xml_output .= "\t<category name='$name'>\n"; $xml_output .= "\t\t<foto pad='$pad' titel='$titel' width='$width' />\n"; // Escaping illegal characters $row['titel'] = str_replace(">", ">", $row['titel']); $row['titel'] = str_replace("\"", """, $row['titel']); $xml_output .= "\t</category>\n"; } $vorige_categorie = $huidige_categorie; } $xml_output .= "</gallery>"; /* $file= fopen("myFile.xml", "w"); fwrite($file, $xml_output); fclose($file); print "<meta http-equiv=\"refresh\"content=\"0;URL=update_ok.php\">"; exit(); */ ?>
Met deze code krijg ik een xml zoals dit :
<?xml version="1.0" ?>
<gallery>
<category name="interieur">
<foto pad="Image_9391_LR.jpg" titel="" width="0" />
</category>
<category name="interieur2">
<foto pad="Image_9406_LR.jpg" titel="" width="0" />
</category>
<foto pad="Image_9399_LR.jpg" titel="" width="0" />
<foto pad="Image_9421_LR.jpg" titel="" width="0" />
</gallery>
<?xml version="1.0" ?> <gallery> <category name="interieur"> <foto pad="Image_9391_LR.jpg" titel="" width="0" /> </category> <category name="interieur2"> <foto pad="Image_9406_LR.jpg" titel="" width="0" /> </category> <foto pad="Image_9399_LR.jpg" titel="" width="0" /> <foto pad="Image_9421_LR.jpg" titel="" width="0" /> </gallery>
MAAR MIJN UITEINDELIJKE OUTPUT ZOU DIT MOETEN KUNNEN WORDEN ;
<?xml version="1.0" encoding="utf-8"?>
<gallery>
<category name="PROJECT 1 //">
<foto pad="images/5/Image_9391_LR.jpg" titel="Image_9399_LR" width="750" />
</category>
<category name="PROJECT 2 //">
<foto pad="images/5/Image_9399_LR.jpg" titel="Image_9399_LR" width="750" />
<foto pad="images/5/Image_9406_LR.jpg" titel="Image_9399_LR" width="750" />
<foto pad="images/5/Image_9421_LR.jpg" titel="Image_9399_LR" width="750" />
</category>
</gallery>
<?xml version="1.0" encoding="utf-8"?> <gallery> <category name="PROJECT 1 //"> <foto pad="images/5/Image_9391_LR.jpg" titel="Image_9399_LR" width="750" /> </category> <category name="PROJECT 2 //"> <foto pad="images/5/Image_9399_LR.jpg" titel="Image_9399_LR" width="750" /> <foto pad="images/5/Image_9406_LR.jpg" titel="Image_9399_LR" width="750" /> <foto pad="images/5/Image_9421_LR.jpg" titel="Image_9399_LR" width="750" /> </category> </gallery>
Hoe los ik dit op ? |
12 antwoorden
|
|
|
offline
|
PHP interesse
|
|
TekRequest – 09/06/2009 16:41 (Laatst gewijzigd op 09/06/2009 16:42)
|
|
offline
|
Nieuw lid
|
en waarom zou ik dat doen? Ik zit nu al zo ver ...
enkel de logica ontbreekt me een beetje in dit gedeelte :
for($i = 0 ; $i < mysql_num_rows($resultID) ; $i++)
{
$row = mysql_fetch_assoc($resultID);
$pad = $row['pad'];
$titel = $row['titel'];
$width = $row['width'];
$name = $row['naam'];
$huidige_categorie = $row['categorie_id'];
// echo $categorie_id;
// ALS CATEGORIE VERANDERT , NIEUWE CATEGORIE OPSTARTEN
if ($huidige_categorie == $vorige_categorie) {
$xml_output .= "\t\t<foto pad='$pad' titel='$titel' width='$width' />\n";
// Escaping illegal characters
$row['titel'] = str_replace("&", "&", $row['titel']);
$row['titel'] = str_replace("<", "<", $row['titel']);
$row['titel'] = str_replace(">", ">", $row['titel']);
$row['titel'] = str_replace("\"", """, $row['titel']);
} else if ($huidige_categorie != $vorige_categorie) {
$xml_output .= "\t<category name='$name'>\n";
$xml_output .= "\t\t<foto pad='$pad' titel='$titel' width='$width' />\n";
// Escaping illegal characters
$row['titel'] = str_replace("&", "&", $row['titel']);
$row['titel'] = str_replace("<", "<", $row['titel']);
$row['titel'] = str_replace(">", ">", $row['titel']);
$row['titel'] = str_replace("\"", """, $row['titel']);
$xml_output .= "\t</category>\n";
}
$vorige_categorie = $huidige_categorie;
}
$xml_output .= "</gallery>";
{ $pad = $row['pad']; $titel = $row['titel']; $width = $row['width']; $name = $row['naam']; $huidige_categorie = $row['categorie_id']; // echo $categorie_id; // ALS CATEGORIE VERANDERT , NIEUWE CATEGORIE OPSTARTEN if ($huidige_categorie == $vorige_categorie) { $xml_output .= "\t\t<foto pad='$pad' titel='$titel' width='$width' />\n"; // Escaping illegal characters $row['titel'] = str_replace(">", ">", $row['titel']); $row['titel'] = str_replace("\"", """, $row['titel']); } else if ($huidige_categorie != $vorige_categorie) { $xml_output .= "\t<category name='$name'>\n"; $xml_output .= "\t\t<foto pad='$pad' titel='$titel' width='$width' />\n"; // Escaping illegal characters $row['titel'] = str_replace(">", ">", $row['titel']); $row['titel'] = str_replace("\"", """, $row['titel']); $xml_output .= "\t</category>\n"; } $vorige_categorie = $huidige_categorie; } $xml_output .= "</gallery>";
iemand die hier een suggestie op kan doen ?
|
|
|
|
offline
|
PHP interesse
|
TekRequest schreef: en waarom zou ik dat doen? Ik zit nu al zo ver ...
Omdat je rotzooi krijgt die niet klopt.
|
Flex1986 – 09/06/2009 18:37 (Laatst gewijzigd op 09/06/2009 18:38)
|
|
offline
|

Senior Member
|
TekRequest schreef: en waarom zou ik dat doen? Ik zit nu al zo ver ...
Als je ook maar een beetje research had gedaan dan was je al gelijk op PHP.net: simplexml uitgekomen. Een zoekactie naar xml op php.net geeft al simplexml als resultaat.
Het voordeel van simplexml is dat het makkelijker uit te breiden is en als je het goed toepast is het ook nog een valid XML wat bij jouw script nog maar de vraag is.
|
|
|
|
offline
|
Nieuw lid
|
Ok bedankt voor de suggesties, ik probeer het eens met simple xml .
|
|
|
|
offline
|
Lid
|
Ik heb laatst ook gegevens uit mij database gehaald en omgezet naar XML. Ik heb dit toegepast op het simpleviewer fotoalbum.
Mijn topic
Misschien heb je er iets aan.
Suc6
|
|
|
|
offline
|
Nieuw lid
|
Ok heb het nu geprobeerd via simple xml, maar werkt nog niet helemaal , waarom wordt hier men category gesloten voor hij wordt opgevuld ?
xml Output :
<?xml version="1.0"?>
<gallery>
<category name="test"/>
<foto pad="Image_9391_LR.jpg" titel="titel 1" width="0"/>
<foto pad="Image_9399_LR.jpg" titel="titel 3" width="0"/>
<foto pad="Image_9406_LR.jpg" titel="titel 2" width="0"/>
<category name="test2"/>
<foto pad="Image_9421_LR.jpg" titel="" width="0"/>
</gallery>
<?xml version="1.0"?> <gallery> <category name="test"/> <foto pad="Image_9391_LR.jpg" titel="titel 1" width="0"/> <foto pad="Image_9399_LR.jpg" titel="titel 3" width="0"/> <foto pad="Image_9406_LR.jpg" titel="titel 2" width="0"/> <category name="test2"/> <foto pad="Image_9421_LR.jpg" titel="" width="0"/> </gallery>
php Output :
if ($huidige_categorie == $vorige_categorie) {
$make = $xml->addChild('foto');
$make->addAttribute('pad', htmlspecialchars(utf8_encode($pad)));
$make->addAttribute('titel', htmlspecialchars(utf8_encode($titel)));
$make->addAttribute('width', htmlspecialchars(utf8_encode($width)));
} else if ($huidige_categorie != $vorige_categorie) {
$make = $xml->addChild('category');
$make->addAttribute('name', htmlspecialchars(utf8_encode($name)));
$make = $xml->addChild('foto');
$make->addAttribute('pad', htmlspecialchars(utf8_encode($pad)));
$make->addAttribute('titel', htmlspecialchars(utf8_encode($titel)));
$make->addAttribute('width', htmlspecialchars(utf8_encode($width)));
}
if ($huidige_categorie == $vorige_categorie) { $make = $xml->addChild('foto'); } else if ($huidige_categorie != $vorige_categorie) { $make = $xml->addChild('category'); $make = $xml->addChild('foto'); }
|
Flex1986 – 12/06/2009 11:34 (Laatst gewijzigd op 12/06/2009 11:35)
|
|
offline
|

Senior Member
|
Ik weet niet of het er mee te maken heeft maar je XML is totaal niet valid. Je kan niet zomaar zelf attributen gaan verzinnen en toevoegen.
<foto>
<pad></pad>
<titel></titel>
<width></width>
</foto>
<foto> <pad></pad> <titel></titel> <width></width> </foto>
Zo zou je je XML op moeten bouwen.
Op de code volgens mij gaat die in je else fout. Je moet namelijk je foto child toevoegen aan je $make variabele.
|
|
|
|
offline
|
Nieuw lid
|
Haaj Flex,
Als ik er een eigen DTD voor zou schrijven, zou dit wel valid zijn, dat is het momenteel nog niet, dat klopt, maar dat heeft niks te maken met het feit dat de output nog niet is zoals wat ik ervan verwacht.
Wat bedoel je met : "Je moet namelijk je foto child toevoegen aan je $make variabele"
Kan je me verder op weg helpen pls? zou vriendelijk zijn ! thanks
|
|
|
|
offline
|

Senior Member
|
if ($huidige_categorie == $vorige_categorie) {
$foto = $xml->addChild('foto');
$foto->addAttribute('pad', htmlspecialchars(utf8_encode($pad)));
$foto->addAttribute('titel', htmlspecialchars(utf8_encode($titel)));
$foto->addAttribute('width', htmlspecialchars(utf8_encode($width)));
} else if ($huidige_categorie != $vorige_categorie) {
$make = $xml->addChild('category');
$make->addAttribute('name', htmlspecialchars(utf8_encode($name)));
$foto = $make->addChild('foto'); //$make aanroepen in plaats van $xml anders beland hij niet in de categorie.
$foto->addAttribute('pad', htmlspecialchars(utf8_encode($pad)));
$foto->addAttribute('titel', htmlspecialchars(utf8_encode($titel)));
$foto->addAttribute('width', htmlspecialchars(utf8_encode($width)));
}
if ($huidige_categorie == $vorige_categorie) { $foto = $xml->addChild('foto'); } else if ($huidige_categorie != $vorige_categorie) { $make = $xml->addChild('category'); $foto = $make->addChild('foto'); //$make aanroepen in plaats van $xml anders beland hij niet in de categorie. }
Na de else wat aanpassingen gemaakt. Staat commentaar in en om het duidelijker te maken heb ik de variabele verandert in de naam van het element waarmee je bezig bent.
|
Dit onderwerp is gesloten.
|
|
|