Assistance please
am 03.04.2008 11:08:26 von derekblakeyNOSPAMHello
I would appreciate some assistance please with a web site I help out with.
The website is database driven with MYSQL as the back end.
The web site only holds about 450 records which contain stories. The web
site allows the user to select from various lists either a full list of all
of the stories or a sub set of the stories based on various select
statements, this works well and has been for some time.
The problem was that to move from one story to the next required the user to
go back to the list and select the next story, there was no forward or back
buttons.
I was asked to add these and on the face of it, it appeared to be fairly
simple but clearly the scripts needed to know which story to go to next, it
could not just go to the next in the database.
The user selects a story from a list on one page of the web site, the info
is then transferred by $_POST to the web page that will display all of the
data.
My idea was to build an array of which stories were required in the first
page and pass this via $_POST to the main display page. This works for MOST
of the stories BUT and it is a big BUT when the script gets to a part of the
database where several records have been deleted alsorts of strange things
start to happen.
For example no data is shown. One pages where it works OK I have it
,temporarily showing how big the array for this selection is, where in the
array the current story is but once a user selects forward to get to a story
within the records that have been deleted the array size and position within
the array lose their values.
The database starts with story number 1 being record ID 1 etc but at about
story 50 records have been deleted and the story number is one or two number
out from the ID number. The story number do exist but not with the
correspondng ID number. So story 50 may be record 55.
I have written a script to show what is in the array and all of the items
appear to be OK and in the correct order etc.
I can not see why this approach does not work and it is driving me nuts.
Any assistance would be appreciated and maybe if anyone thinks that this is
not the best way to acheive what I want advice would be greatfully received.
You can test the cose at http://www.zitech.co.uk/nslc . Select the Memories
option in the left vertical menu and then List All Memories at the right.
Only the Next button works (?) at the moment.
I have included code here in two parts, the first is the first web page
which creates the array and the second is the page that displays the data. I
appologise it is a bit of a mess but I have been fiddling around with this
for a while trying to get it to work.
Many thanks
Derek
------------------------------------------------
$sqlstring= "select * from mems2 order by memoryno";
$result=mysql_query($sqlstring) or die('Could not find the table');
//*****************************************************
// Do list
//*****************************************************
echo ("Select a memory from this list (Memories
showing ** contain a photograph)");
echo ("
");
echo ("
echo (" ");
echo ("
include 'closedb.php';
?>
------------------------------------------------------------ ---------------------------------
include 'opendb.php';
//pass the value sent from the listallmemories page into whichmemory
$whichmemory = $_POST["selectedmemory"];
//********************************************************** **********************
// 18 03 08
// Added the following $_POST lines to catch the data from nextmemory.php
//********************************************************** ***********************
$founditinarray = $_POST['founditinarray'];
$memory_array = unserialize(urldecode($_POST["memory_array"]));
$arraysize = $_POST['arraysize'];
//********************************************************** *********
// 18 03 08 - testing code only
// check to see if the memory number is being loaded into the array
// Data shown correctly to this point
//
echo "Array size = " . $arraysize;
echo "
";
//$arrayposition = 0;
//while ($arrayposition<=($arraysize -1) )
//{
//echo " Array position : " . $arrayposition . " Array value held : "
..$memory_array[$arrayposition] ;
//echo "
";
//
//$arrayposition = $arrayposition + 1;
//}
//********************************************************** *****************
//********************************************************** *****************
//********************************************************** ********************
// 05 Feb 2008
// Find the position within the array of the selected memory
//********************************************************** ********************
$counter = 0;
while ( $counter <= ($arraysize))
{
//********************************************************** ******************************************
// 26 March 2008
// Extra IF statement added to see if we can remove blank or deleted
database records before they are
// added to the array. Allowing the blank/deleted record causes blank
screens to be displayed
// in memory
//********************************************************** ********************************************
//echo "
";
//echo "String length of memory_array : " .
strlen("$memory_array[$counter]") . " Value : " . $memory_array[$counter];
if (strlen("$memory_array[$counter]") > 0)
{
if ($memory_array[$counter] == $whichmemory)
{
//********************************************************** **********************
//Store the position in the array for future use
//********************************************************** **********************
$founditinarray = $counter;
}
}
$counter = $counter + 1;
}
echo "founditinarray : " . $founditinarray;
echo "
";
echo "arraysize : " . $arraysize;
echo "
";
//echo "counter " . $counter;
//echo "
";
//echo "arraysize : " . $arraysize;
//echo "
";
//if the value in selectedmemory > 0 then we must have come from the
listallmemories page
//so use the value to query the database
// else
// read the cookie and get the value of the memory from there. We must
have come back after using switcher
if ($selectedmemory > 0)
{
$sqlstring= "select * from mems2 where id = $whichmemory";
} else {
$whichmemory = $_COOKIE["whichmemory"];
$sqlstring= "select * from mems2 where id = $whichmemory";
}
$result=mysql_query($sqlstring) or die('could not find $sqlstring');
$myrow=mysql_fetch_row($result);
//Add an entry to the audit database
//Set up date and time created values used in date/time created and
date/timeamended
$dateamended= date("y.m.d");
$timeamended = date('H:i:s' );
$remoteaddress = $_SERVER['REMOTE_ADDR'];
$browser = $_SERVER["HTTP_USER_AGENT"];
$hostname = gethostbyaddr($_SERVER['REMOTE_ADDR']);
$sqlstring ="INSERT INTO `audit` ( id , memoryno ,date , time , audittext
, auditcode, ipaddress , browser, dnsname ) VALUES ( '$id' , '$myrow[1]' ,
'$dateamended', '$timeamended', 'memory viewed' , '5' , '$remoteaddress' ,
'$browser' , '$hostname')";
$result=mysql_query($sqlstring)or die("Could not find a recordset for
......".$sqlstring);
// This is the title
echo "Memory No ";
echo $myrow[1];
echo " - ";
echo $myrow[2];
?>
//********************************************************** ***************************************************
// 28/02/08
// Next and previous buttons removed from template and coded here, just
above memory text.
//********************************************************** ****************************************************
echo "
";
echo"
"; // If $founditinarray > 0 then we must at the second or subsiquent memory - show button // IF $founditinarray is not greater than 0 then we must be at the first memory - do not show button if ($founditinarray > 0) { echo ""; } echo " | ";"; | "; echo "
action='memory.php'>"; ";echo ""; //********************************************************** ************************ // 25 03 08 // Moved code from the nextmemory.php script in to here //********************************************************** ************************ // Increment the position number of the reqwuired memory within the array $founditinarray = $founditinarray + 1; // Now find the new memory number $count = 0; while ($count <= $founditinarray) { $whichmemory = $memory_array[$count]; //echo " "; //echo "Found memory number : " . $newmemorynumber; //echo " "; $count ++; } //********************************************************** ************************* //********************************************************** ************************* //****************************************************** // Need to sent out the following variables //******************************************************* $memory_array = urlencode(serialize($memory_array)); echo " '$memory_array'> "; echo " "; echo " '$whichmemory'> "; echo " '$founditinarray'> "; echo " echo " | ";