Jump to content

If/Else counter problem


Daegalus

Recommended Posts

Ok, I've been at this for 2 hours now and I can't for hte life of me figure out why this isn't working.

 

I have a site using PHP and Javascript intertwined to make it work like AJAX but without the hassle of XMLHTTPRequest and all. So onto the problem.

 

So hear is the stuff in the main page:

 

<script type="text/javascript">
	   // Get base url
		url = document.location.href;
		xend = url.lastIndexOf("/") + 1;
		var base_url = url.substring(0, xend); 		

		function changeTestimonialPrev() {
		        // Create new JS element
				  <?php if($testimnum == 0) { $testimnum = $testimsize-1; } else { $testimnum--; } ?>
		        var jsel = document.createElement('SCRIPT');
		        jsel.type = 'text/javascript';
		        jsel.src = base_url + 'includes/js.php?testimprev=' + <?php echo $testimnum; ?>;

		        // Append JS element (therefore executing the 'AJAX' call)
		        document.body.appendChild (jsel);
				  
		}
		function changeTestimonialNext() {
		        // Create new JS element
				  <?php if($testimnum == $testimsize-1) { $testimnum = 0; } else { $testimnum++; } ?>
		        var jsel = document.createElement('SCRIPT');
		        jsel.type = 'text/javascript';
		        jsel.src = base_url + 'includes/js.php?testimnext=' + <?php echo $testimnum; ?>;

		        // Append JS element (therefore executing the 'AJAX' call)
		        document.body.appendChild (jsel);
				  
		}
	</script>

 

and here is the PHP code in teh js.php file.

 

(this tag isnt really here, i just did it so it will do the proper syntax highlighting) --> <?php 
$testimprev = $_GET['testimprev'];
if(isset($_GET['testimprev']))
{
?>
	  		document.getElementById("testimauthor").childNodes[0].childNodes[0].data = "<?php echo $testimonials[$testimprev]['name']; ?>";
			document.getElementById("testimauthor").childNodes[3].data = "<?php echo $testimonials[$testimprev]['affiliation']; ?>";
			document.getElementById("testimtext").childNodes[0].data = "<?php echo $testimonials[$testimprev]['body']; ?>";
<?php
for($i=0; $i<5; $i++) {
	if($i<$testimonials[$testimnprev]['rating'])
	{ ?>
			document.getElementById("star<?php echo $i+1; ?>").childNodes[0].setAttribute("src","images/yellow-star.png");
<?php
	}
	else
	{ ?>
			document.getElementById("star<?php echo $i+1; ?>").childNodes[0].setAttribute("src","images/black-star.png");
<?php
	}
}
}
$testimnext = $_GET['testimnext'];
if(isset($_GET['testimnext']))
{
?>
	  		document.getElementById("testimauthor").childNodes[0].childNodes[0].data = "<?php echo $testimonials[$testimnext]['name']; ?>";
			document.getElementById("testimauthor").childNodes[3].data = "<?php echo $testimonials[$testimnext]['affiliation']; ?>";
			document.getElementById("testimtext").childNodes[0].data = "<?php echo $testimonials[$testimnext]['body']; ?>";
<?php
for($i=0; $i<5; $i++) {
	if($i<$testimonials[$testimnext]['rating'])
	{ ?>
			document.getElementById("star<?php echo $i+1; ?>").childNodes[0].setAttribute("src","images/yellow-star.png");
<?php
	}
	else
	{ ?>
			document.getElementById("star<?php echo $i+1; ?>").childNodes[0].setAttribute("src","images/black-star.png");
<?php
	}
}
}

 

The problem is, when I click on the 2 buttons i have for prev and next. it only stays at the first and last items. I click Next and it shows the first one. and then blocks me from continueing. If i press prev it goes to the prev one, (which is hte last one) but then won't go any further. SO I assume its with my counter conditions at the beginning of prev and next, but I cant for the life of me figure out why it says on the end points nad wont go into the ones inbetween the first and last.

 

Now Im not used to trinary so thats why i don't use it, and also I hate using echo to output html/jscript to the sourcecode, so dont comment on the closing and opening of PHP so many times please, its mainly for my sanity.

 

The information is stored in a database that is run, and stored into an Array at the beginning of the file. form there I use the array for everything else.

Link to comment
https://forums.phpfreaks.com/topic/97041-ifelse-counter-problem/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.