Jump to content

Help required to get back/next buttons to cycle through list sequentially


Recommended Posts

Hi, after hours and hours of reading and research and *nearly* getting there, I've had to resort to asking for help with this problem! I'm a newbie to PHP so suspect the solution might be quite obvious, and apologies if I explain myself badly. Anyway...

 

I have a portfolio page with a list of clients names. When a client is clicked on, their info is displayed via a function. I want to be able to have a 'back' and 'next' button for each client so users can cycle through the list sequentially. I don't want to duplicate the buttons for every client session, though.

 

So I need to tell the back and next links what to link to for each client. My attempt at doing this is in the stripped down version of my code, below. Only it doesnt work. If anyone could help me I'd be hugely grateful! If I haven't been clear in my intentions or the code below is gibberish, please let me know and I'll try and explain myself better!!  :)

 

Incidentally, the page is called via the site navigation showing the 'clientlist'  function, from which the user can select a client. 'subnav' is also displayed, and this contains the back/next buttons.

 

<?php
$client = trim($_GET['client']);
$subnav = trim($_GET['subnav']);


function clientlist($str){
    echo 'PLEASE CLICK ON A CLIENT\'S NAME TO VIEW PHOTOGRAPHS OF OUR WORK:<br /><br />
		<"?client=client1&subnav=subnav">client 1</a><br />
		<"?client=client2&subnav=subnav">client 2</a><br />
		<"?client=client3&subnav=subnav">client 3</a><br />
		<"?client=client4&subnav=subnav">client 4</a><br />
		<"?client=client5&subnav=subnav">client 5</a><br />';
}

function client1($str){
    echo 'client 1 info here';
}

function client2($str){
    echo 'client 2 info here';
}

function client3($str){
    echo 'client 3 info here';
}

function client4($str){
    echo 'client 4 info here';
}

function client5($str){
    echo 'client 5 info here';
}


function subnav($str){
if ($client == 'client1') { 
$back = 'none';
$next = 'client2';
}
if ($client == 'client2') { 
$back = 'client1';
$next = 'client3';
}	
if ($client == 'client3') { 
$back = 'client2';
$next = 'client4';
}		
if ($client == 'client4') { 
$back = 'client3';
$next = 'client5';
}		
if ($client == 'client5') { 
$back = 'client4';
$next = 'none';
}		
$client = trim($_GET['client']);
$footer = trim($_GET['back']);
$subnav = trim($_GET['next']);
echo "<\"?subnav=subnav&client=$back\"><img id=\"back\" src=\"img/nav_back.gif\" alt=\"back to previous image\" width=\"60\" height=\"20\" /></a>  <a\"?subnav=subnav&client=$next\"><img id=\"next\" src=\"img/nav_next.gif\" alt=\"next image\" width=\"60\" height=\"20\" /></a>";
}
?>




<html>
<head>
<title>Portfolio</title>
<?php include 'header.php';?>
</head>

<body>
<div id="content">			

	<?php
	switch($client){
	    case "clientlist":
	        clientlist($text);
	        break;
	    case "client1":
	        client1($text);
	        break;		
	    case "client2":
	        client2($text);
	        break;	
	    case "client3":
	        client3($text);
	        break;			
	    case "client4":
	        client4($text);
	        break;	
	    case "client5":
	        client5($text);
	        break;	}
	?>		

</div>	

<div id="subnav" />		

	<?php
	switch($subnav){
	    case "subnav":
	        subnav($text);
	    break;}
    ?>

</div>


</div>
</div>	
</body>
</html>

 

Try this:

 

<?php

$clientData = array();
$clientData[0] = 'client 1 info here';
$clientData[1] = 'client 2 info here';
$clientData[2] = 'client 3 info here';
$clientData[3] = 'client 4 info here';
$clientData[3] = 'client 5 info here';

$clientID = trim($_GET['clientID']) - 1;


function clientlist($str){
    echo "PLEASE CLICK ON A CLIENT'S NAME TO VIEW PHOTOGRAPHS OF OUR WORK:<br /><br />";
    foreach ($clientID=0; $clientID<count($clientData); $clientID++) {
        echo "<a href=\"?clientID=$clientID&subnav=subnav\">Client " . ($clientID + 1) . "</a><br>\n";

    }
}

function displayClient($text){
    global $clientData, $clientID;
    echo $clientData[$clientID];
}

?>

<html>
<head>
<title>Portfolio</title>
<?php include 'header.php';?>
</head>

<body>

    <div id="content">		
    <?php
if (isset($clientID) && in_array($clientID, $clientData)) {
	displayClient($text);
} else {
	clientlist($str);
}
    ?>		
    </div>	


    <div id="subnav" />		
    <?php
//Display the back link if not the first client
if ($clientID > 1) {
	echo "<a href=\"?subnav=subnav&clientID=".($clientID-1)."\">";
                echo "<img id=\"back\" src=\"img/nav_back.gif\" alt=\"back to previous image\" width=\"60\" height=\"20\" />";
	echo "</a>";
}

        echo "  ";

//Display the forward link if not the last client
if ($clientID < count()) {
	echo "<a href=\"?subnav=subnav&clientID=".($clientID+1)."\">";
                echo "<img id=\"next\" src=\"img/nav_next.gif\" alt=\"next image\" width=\"60\" height=\"20\" />";
	echo "</a>";
}

    ?>
    </div>

</body>
</html>

mjdamato - thanks a lot for the code, i think i see what you've done there. Unfortunately I'm getting the following error when i try it out:

 

Parse error: syntax error, unexpected T_STRING, expecting '(' on line 15

 

Line 15 is marked with *** below - I can't work out why its expecting a '(' !!

 

function clientlist($str){
    echo "PLEASE CLICK ON A CLIENT'S NAME TO VIEW PHOTOGRAPHS OF OUR WORK:<br /><br />";
     ***foreach ($clientID=0; $clientID<count($clientData); $clientID++) {***
        echo "<a href=\"?clientID=$clientID&subnav=subnav\">Client " . ($clientID + 1) . "</a><br>\n";

    }
}

 

I thought foreach should, perhaps, be for each (ie, with a space) so tried this, but it didn't solve anything...

 

 

thanks - code replaced! I appreciate you taking the time to help me out, by the way.

 

The page is kinda working but doesnt seem to want to diplay the client list. Heres a link to the test page incase you want to have a look:

 

http://www.millingtonassociates.com/test/test.php

 

Again, I decided to change directions while I was writing that and did not complete the change properly. Here is the updated code for that along with a fix for the subNav which shouldn't show when you are not on a client page:

<?php

$clientData = array();
$clientData[0] = 'client 1 info here';
$clientData[1] = 'client 2 info here';
$clientData[2] = 'client 3 info here';
$clientData[3] = 'client 4 info here';
$clientData[3] = 'client 5 info here';

$clientID = trim($_GET['clientID']) - 1;


function displayClientList($str){
    echo "<div id=\"content\">";
    echo "PLEASE CLICK ON A CLIENT'S NAME TO VIEW PHOTOGRAPHS OF OUR WORK:<br /><br />";
    foreach ($clientData as $clientID=>$clientText) {
        echo "<a href=\"?clientID=$clientID&subnav=subnav\">Client " . ($clientID + 1) . "</a><br>\n";
    }
    echo "</div>";
}

function displayClient($text){
    global $clientData, $clientID;

    echo "<div id=\"content\">";
    echo $clientData[$clientID];
    echo "</div>";
}

function displaySubNav($clientID) {
    echo "<div id=\"subnav\">";

    //Display the back link if not the first client
    if ($clientID > 1) {
        echo "<a href=\"?subnav=subnav&clientID=".($clientID-1)."\">";
        echo "<img id=\"back\" src=\"img/nav_back.gif\" alt=\"back to previous image\" width=\"60\" height=\"20\" />";
        echo "</a>";
    }

    echo "  ";

    //Display the forward link if not the last client
    if ($clientID < count()) {
        echo "<a href=\"?subnav=subnav&clientID=".($clientID+1)."\">";
        echo "<img id=\"next\" src=\"img/nav_next.gif\" alt=\"next image\" width=\"60\" height=\"20\" />";
        echo "</a>";
    }
}

?>

<html>
<head>
    <title>Portfolio</title>
    <?php include 'header.php';?>
</head>

<body>

<?php
    if (isset($clientID) && in_array($clientID, $clientData)) {
        displayClient($text);
        displaySubNav($clientID);
    } else {
        displayClientList($str);
    }
?>		

</body>
</html>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.