I'm new in php.
I'm trying to find a way to match the right url dynamically in every description.
For example in the case where the description = Xios i would like the url to become :<li><div id="Left1"><a href="main.php?description=Xios">' . make_safe( $item['description'] ) . '</a></div></li>
Now in the case where the description = Kiklades i would like the url to become : <li><div id="Left1"><a href="main.php?description=Kiklades">' . make_safe( $item['description'] ) . '</a></div></li>
Endly in the case where the description = Mikonos i would like the url to become :<li><div id="Left1"><a href="main.php?description=Mikonos">' . make_safe( $item['description'] ) . '</a></div></li>
foreach ($item_array as $item) {
if ( make_safe($item['diamerisma']) == $_GET['diamerisma'] ) {
if (!in_array($item['description'], $used_values) ) {//This will check if this value hasn't been listed yet
$html .= '<ul data-role="listview" id="weatherList" data-theme="b" data-insert="true" >';
$html .= '<li><div id="Left1"><a href="main.php?description=Κυκλάδες">' . make_safe( $item['description'] ) . '</a></div></li>';
$html .= '</ul>';
$html .= '</dd>';
$used_values[] = $item['description'];//here you preserve used values for avoiding repetition
}
}
}
the xml code
<item>
<title>Xios center</title>
<description>Xios</description>
<diamerisma>Aigaio</diamerisma>
<metar>1010</metar>
</item>
<item>
<title>Siros</title>
<description>Kiklades</description>
<diamerisma>Aigaio</diamerisma>
<metar>1011</metar>
</item>
<item>
<title>Naxos center</title>
<description>Kiklades</description>
<diamerisma>Aigaio</diamerisma>
<metar>1012</metar>
</item>
<item>
<title>Mikonos center</title>
<description>Mikonos</description>
<diamerisma>Aigaio</diamerisma>
<metar>1013</metar>
</item>
Any help will be appreciated!thanks in advance guys!
As I don't know how $item_array
is created, I'll go back one step from your code and create $item_array
in a different way:
$xml = simplexml_load_string($x); // assume XML in $x
$dia = "Aigaio"; // use $_GET... in your code
$item_array = $xml->xpath("//item[diamerisma = '$dia']");
Now, $item_array
contains only <item>
-nodes with the desired <diamerisma>
-child. We do not have to check for <diamerisma>
in the loop:
foreach ($item_array as $item) {
// simplified
echo '<a href="main.php?description=' . $item->description . '">' . $item->description . '</a>';
}
see it working: https://eval.in/120611
I realized there is a very simple answer to your question:
$html .= '<li><div id="Left1"><a href="main.php?description=' . make_safe($item['description']) . '">' . make_safe($item['description']) . '</a></div></li>';