Greetings been stuck on this for a few weeks now. I am trying to create xml with php and mysql. All data is coming back how I need it I am just having trouble pairing the structure up.
This is what I am after where items are grouped however I am not sure how I can check against either a menu name or other(id) to group similar projects:
XML:
<?xml version="1.0" encoding="UTF-8" ?>
<menu>
<item name="Testing For Menu" action="Testing For Menu" client="Testing For Menu" project="This is a test " >
<sub name="Project Level 2" action="Project Level 2" client="Project Level 2" project="This is project level 2" >
<subsub name="This is file level" action="This is file level" client="This is file level" project="" path="docs/f1.rtf" pid="41" />
<subsub name="This is file level" action="This is file level" client="This is file level" project="" path="docs/f2.rtf" pid="42" />
</sub>
<item name="Testing For Menu 3" action="Testing For Menu 3" client="Testing For Menu 3" project="Testing for menu 3 content" >
<sub name="Project Level 3" action="Project Level 3" client="Project Level 3" project="Project Level 3" >
<subsub name="This is file level 3" action="This is file level 3" client="This is file level 3" project="" path="docs/f3.rtf" pid="43" />
<subsub name="Project Level 3 a" action="Project Level 3 a" client="Project Level 3 a" project="" path="docs/f4" pid="49" />
</sub>
</menu>
This is what I am getting currently where items are all individually written out instead of grouped:
XML:
<?xml version="1.0" encoding="UTF-8" ?>
<menu>
<item name="Testing For Menu" action="Testing For Menu" client="Testing For Menu" project="This is a test " >
<sub name="Project Level 2" action="Project Level 2" client="Project Level 2" project="This is project level 2" >
<subsub name="This is file level" action="This is file level" client="This is file level" project="" path="docs/f1.rtf" pid="41" />
</sub>
</item>
<item name="Testing For Menu" action="Testing For Menu" client="Testing For Menu" project="This is a test " >
<sub name="Project Level 2" action="Project Level 2" client="Project Level 2" project="This is project level 2" >
<subsub name="This is file level" action="This is file level" client="This is file level" project="" path="docs/f2.rtf" pid="42" />
</sub>
</item>
<item name="Testing For Menu 3" action="Testing For Menu 3" client="Testing For Menu 3" project="Testing for menu 3 content" >
<sub name="Project Level 3" action="Project Level 3" client="Project Level 3" project="Project Level 3" >
<subsub name="This is file level 3" action="This is file level 3" client="This is file level 3" project="" path="docs/f3.rtf" pid="43" />
</sub>
</item>
<item name="Testing For Menu 3" action="Testing For Menu 3" client="Testing For Menu 3" project="Testing for menu 3 content" >
<sub name="Project Level 3" action="Project Level 3" client="Project Level 3" project="Project Level 3" >
<subsub name="Project Level 3 a" action="Project Level 3 a" client="Project Level 3 a" project="" path="docs/f4" pid="49" />
</sub>
</item>
</menu>
Attached is my php script I wrote. It works fine however doesnt group. What kind of logic can I use to do this? Should I use a for, while or some other loop to see if same project? Any advice helps. Thanks in advance. MT
How can I only return unique results or check some how against the last $row[‘project_name’] and if its the same then dont output but if its different carry on? This I think will solve my issue and then I can implement for each level of xml. But how can you check against a ($variable - 1) and its information in php array?
…
$result = mysql_query($query, $connection);
while ($row = mysql_fetch_array($result)) {
echo $row[‘project_name’];
}
Where it would return:
Project 1
Testing 1
File 1
Project 2
Testing 2
File 1
Project 3
Testing 3
File 1
Project 3
Testing 3
File 2
How can I run a check on a while loop so that it only kicks back:
Project 1
Testing 1
File 1
Project 2
Testing 2
File 1
Project 3
Testing 3
File 1
File 2
$cnt = 25; // set it to length of dataset
while ($cnt > -1) {
if ($row[$cnt] == $row[($cnt-1)]) {
echo "moo";
}
else {
echo "quack";
}
cnt--;
}
Almost there, got it to the number of original <item> level with no duplicates, however I am trying to iterate through $row to match up the <sub> and <subsub> levels.
Shows no duplicates, but once i try to do my while loops after the first has been set, it will add the next level, and following level; but wont add any additional lower levels if in data set. Is there a better way to do this? Been trying here and there on this in spare time for while to no avail. personal gallery using flash xml menu here on Kirupa.
Here is current:
$num = mysql_num_rows($result);
if ($num != 0) {
$_xml ="<?xml version=\"1.0\" encoding=\"UTF-8\" ?>
";
$_xml .="<menu>
";
$loopCt = 0;
while($row = mysql_fetch_array($result)) {
if ($loopCt == 0) {
$_xml .=" <item name=\"" . $row['subject_name'] . "\" action=\"" . $row['subject_name'] . "\" >
";
$_xml .=" <sub name=\"" . $row['menu_name'] . "\" action=\"" . $row['menu_name'] . "\" >
";
$_xml .=" <subsub name=\"" . $row['menuName'] . "\" action=\"" . $row['menuName'] . "\" />
";
$_xml .=" </sub>
";
$_xml .=" </item>
";
$lastMenu = $row['menu_name'];
$lastSubject = $row['subject_name'];
$loopCt++;
} elseif ($row['subject_name'] != $lastSubject) {
$_xml .=" <item name=\"" . $row['subject_name'] . "\" action=\"" . $row['subject_name'] . "\" >
";
while ($row['subject_name'] != $lastSubject) {
$_xml .=" <sub name=\"" . $row['menu_name'] . "\" action=\"" . $row['menu_name'] . "\" >
";
while ($row['menu_name'] != $lastMenu) {
$_xml .=" <subsub name=\"" . $row['menuName'] . "\" action=\"" . $row['menuName'] . "\" />
";
$lastMenu = $row['menu_name'];
}
$lastSubject = $row['subject_name'];
}
$_xml .=" </sub>
";
$_xml .=" </item>
";
} else {
$lastSubject = $row['subject_name'];
}
}
$_xml .="</menu>
";
echo($_xml);
} else {
echo "No Records found";
}
Here it is or what I came up with. This only works for 1 level per main level. I am working on the multiple second levels now. Some extra stuff in there for testing. It is ugly but it does the trick. Anyone come up with a better way let me know.
$num = mysql_num_rows($result);
if ($num != 0) {
$_xml ="<?xml version=\"1.0\" encoding=\"UTF-8\" ?>
";
$_xml .="<menu>
";
$headCt = 0;
$loopCt = 0;
$matchCt = 0;
while($row = mysql_fetch_array($result)) {
$loopCt ++;
echo "This is Loop -- " . $loopCt . " ";
if ($row['subject_name'] != $lastSubject) {
if ($matchCt != 0) {
$_xml .=" </sub>
";
$_xml .=" </item>
";
} else {
}
$matchCt = 0;
$headCt++;
echo "This is Head -- " . $headCt . " ";
$_xml .=" <item name=\"" . $row['subject_name'] . "\" action=\"" . $row['subject_name'] . "\" >
";
$_xml .=" <sub name=\"" . $row['menu_name'] . "\" action=\"" . $row['menu_name'] . "\" >
";
$_xml .=" <subsub name=\"" . $row['menuName'] . "\" action=\"" . $row['menuName'] . "\" />
";
$lastMenu = $row['menu_name'];
$lastSubject = $row['subject_name'];
} elseif ($row['subject_name'] == $lastSubject) {
$matchCt ++;
echo "This is Match -- " . $matchCt . " ";
$_xml .=" <subsub name=\"" . $row['menuName'] . "\" action=\"" . $row['menuName'] . "\" />
";
$lastSubject = $row['subject_name'];
} else {
$lastSubject = $row['subject_name'];
}
}
$_xml .=" </sub>
";
$_xml .=" </item>
";
$_xml .="</menu>
";
echo($_xml);
} else {
echo "No Records found";
}