PDA

View Full Version : Function within a function? I don't think I have this right



Spinethetic
06-19-2008, 01:15 PM
<?php
echo "<h3>Sociology</h3>";
echo "<ul>";
$theuri = $_SERVER['REQUEST_URI']; // Check URL
if (!$myxml=simplexml_load_file('research_sociologyindex.xml')){
echo 'Error reading the XML file';
}
foreach($myxml as $article){
if ( ($theuri =="' . $article->link . '") ) { // If the URL is this page
$itemis = '<li><b>' . $article->title . '</b></li>'; // Disable link if visitor is on this page
} else {
$itemis = '<li><a href=' . $article->link . '>' . $article->title . '</a></li>'; // Generate functional link
}
}
echo "</ul>";
?>


All I want to do is have a list of links that is automatically generated by an external XML file; And when a person is on the page, to disable the link to that page (because he/she is already on it).

The above script dosent return any errors, but it just displays the <h3> title, so I know I'am close; to the annoyance of my girlfriend I was tinkering around with it on a napkin at dinner a bit and of course ended up with a completely different script ... and the 'silent treatment' :D

Best Regards
~Spine :)

Nile
06-19-2008, 01:19 PM
Ok, try this:


<?php
echo "<h3>Sociology</h3>";
echo "<ul>";
$theuri = $_SERVER['REQUEST_URI'];
if ($myxml!=simplexml_load_file('research_sociologyindex.xml')){

die('Error reading the XML file');

}

foreach($myxml as $article){
if ( ($theuri == $article->link) ) {
$itemis ="<li><b> $article->title </b></li>";
} else {
$itemis = "<li><a href=$article->link>$article->title</a></li>";
}
}
echo "</ul>";
?>

Also, can I please see the whole code, that may help...

Spinethetic
06-19-2008, 02:05 PM
Ok, try this:


<?php
echo "<h3>Sociology</h3>";
echo "<ul>";
$theuri = $_SERVER['REQUEST_URI'];
if ($myxml!=simplexml_load_file('research_sociologyindex.xml')){

die('Error reading the XML file');

}

foreach($myxml as $article){
if ( ($theuri == $article->link) ) {
$itemis ="<li><b> $article->title </b></li>";
} else {
$itemis = "<li><a href=$article->link>$article->title</a></li>";
}
}
echo "</ul>";
?>

Also, can I please see the whole code, that may help...

That outputs the same thing as originally. That basically is the whole script. I can include these scripts I use that I deduced I would be able to set this up: research_sociologyindex.xml


<?xml version="1.0" encoding="utf-8" ?>
<sociology>

<article>
<title>Thoughts on the Evolution of Social Inequality: A Paradigmatic Analysis</title>
<description>American anthropology is in a state of theoretical crisis. Unlike most mature scientific disciplines anthropolologists seem unable even to agree on the structure of our discipline or the most important research questions to ask, let alone the best way to develop theory and increase our understanding of the human world. How is productive research possible in such an intellectual climate? In the early 1960ís, philosopher Thomas Kuhn argued that scientific disciplines periodically pass through "revolutionary" changes in theory (1970). According to Kuhn, these changes occur when a dominant paradigm or theoretical framework fails to account for empirical observations and when a better paradigm is developed...</description>
<link>research_sociology_toteosiapa.htm</link>
</article>

<article>
<title>Why we hate complexity</title>
<description>Natural and social systems are complex -- that is, not entirely knowable, unpredictable, resistant to cause-and-effect analysis, in a word, mysterious. For our first three million years on Earth we humans, like every other species on the planet, accepted that mystery. We adapted rather than trying to change our environment. We evolved by learning to accommodate ourselves to our environment. Those unable to accommodate perished....</description>
<link>research_sociology_wwhc.htm</link>
</article>

</sociology>

I have catagory index pages setup using similiar XML files, EXAMPLE (http://www.integralbuddha.net/research_sociology.htm) using code:


<?php
if (!$myxml=simplexml_load_file('research_sociologyindex.xml')){
echo 'Error reading the XML file';
}
foreach($myxml as $article){
echo '<center><img src=','images/upperlotushalf.jpg','></center>';
echo '<blockquote><h3> ' . $article->title . '</h3>';
echo '' . $article->description . '<br />';
echo '<a href=','' . $article->link . '',' class=','button','><span>Continue reading...</span></a></blockquote><br />';
}
?>

rightside_sociologyresearch.php


<?php
echo "<h3 class='leftrightheader'>Sociology</h3>";
$theuri = $_SERVER['REQUEST_URI'];
echo "<ul>";
if ( ($theuri =="/research_sociology_toteosiapa.htm") ) {
$itemis = "<li><b>Thoughts on the Evolution of Social Inequality: A Paradigmatic Analysis</b></a></li>";
} else {
$itemis = "<li><a href='research_sociology_toteosiapa.htm'>Thoughts on the Evolution of Social Inequality: A Paradigmatic Analysis</a></li>";
}
echo $itemis;

if ( ($theuri =="/research_sociology_wwhc.htm") ) {
$itemis = "<li><b>Why we hate complexity</b></a></li>";
} else {
$itemis = "<li><a href='research_sociology_wwhc.htm'>Why we hate complexity</a></li>";
}
echo $itemis;
echo "</ul>";
?>


I figured I would be able to set something up where I wouldent have to constantly update any of the "rightside_whateverresearch.php" files everytime I load a new article.

Best Regards
~Spine :)

Nile
06-19-2008, 07:56 PM
Well, I've never worked with xml files in php, sorry. I just guessed. :-/

josephhamilton1
06-20-2008, 02:59 AM
foreach($myxml as $article){
if ( ($theuri == $article->link) ) {
$itemis ="<li><b> $article->title </b></li>";
} else {
$itemis = "<li><a href=$article->link>$article->title</a></li>";
}

echo $itemis;

}

echo "</ul>";
?>

Maybe I'm missing something here, but why didn't you just echo out the $itemis variable, If you don't print anything it will never display.

Nile
06-20-2008, 03:02 AM
If you look in Spinethetic's previous code, it was echo'd:


<?php
echo "<h3 class='leftrightheader'>Sociology</h3>";
$theuri = $_SERVER['REQUEST_URI'];
echo "<ul>";
if ( ($theuri =="/research_sociology_toteosiapa.htm") ) {
$itemis = "<li><b>Thoughts on the Evolution of Social Inequality: A Paradigmatic Analysis</b></a></li>";
} else {
$itemis = "<li><a href='research_sociology_toteosiapa.htm'>Thoughts on the Evolution of Social Inequality: A Paradigmatic Analysis</a></li>";
}
echo $itemis;

if ( ($theuri =="/research_sociology_wwhc.htm") ) {
$itemis = "<li><b>Why we hate complexity</b></a></li>";
} else {
$itemis = "<li><a href='research_sociology_wwhc.htm'>Why we hate complexity</a></li>";
}
echo $itemis;
echo "</ul>";
?>

Of course that code at the end could be shorted.

Spinethetic
06-20-2008, 03:10 PM
foreach($myxml as $article){
if ( ($theuri == $article->link) ) {
$itemis ="<li><b> $article->title </b></li>";
} else {
$itemis = "<li><a href=$article->link>$article->title</a></li>";
}

echo $itemis;

}

echo "</ul>";
?>

Maybe I'm missing something here, but why didn't you just echo out the $itemis variable, If you don't print anything it will never display.

Because for instance, when I goto article "why we hate complexity" I want the link in the right sidebar to 'gray-out', because the visitor is already on this page. The way I had rightside_sociologyresearch.php set up does in fact work this way. What it is doing now, is indeed loading data from the XML file, but it is not graying out the page links that I am currently on.

Best Regards
~Spine :)

edit: here is one of the two pages I include the script, functioning as I just described: LINK (http://www.integralbuddha.net/research_sociology_wwhc.htm)