kingnutter Posted March 28, 2008 Share Posted March 28, 2008 Hi everyone, I'm trying to create a site that allows me to click through the contents of a MYSQL database one record at a time using code along the lines of $row = mysql_fetch_object($result);. I have created dynamic links at the top of the page as follows: <?php echo <a href="record.php?wid-1">Previous Entry</a><br /> <?php echo <a href="record.php?wid+1">Next Entry</a><br /> (wid being the Primary Key) However the first time the page is parsed I want it to default to record 1 Hopefully you can understand what I am trying to do from the following: if ((!isset($_GET['wid']) || trim($_GET['wid']) == '')) { wid=1 } The statement in braces looks wrong, but I can't find the correct syntax anywhere. Do I need to convert it to a string e.g $record first? Any help appreciated. Many thanks, Gary Quote Link to comment https://forums.phpfreaks.com/topic/98400-assigning-variable-default/ Share on other sites More sharing options...
MadTechie Posted March 28, 2008 Share Posted March 28, 2008 if ( empty($_GET['wid']) ) { $wid=1; } Quote Link to comment https://forums.phpfreaks.com/topic/98400-assigning-variable-default/#findComment-503546 Share on other sites More sharing options...
DyslexicDog Posted March 28, 2008 Share Posted March 28, 2008 It may be better to specify the wid number in get, then create links using php. <?php echo "<a href=\"record.php?wid=".$wid+1."\">Next Record</a>"; echo "<a href=\"record.php?wid=".$wid-1."\">Prevoius Record</a>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/98400-assigning-variable-default/#findComment-503549 Share on other sites More sharing options...
kingnutter Posted March 29, 2008 Author Share Posted March 29, 2008 Thanks for both those pieces of input. I have now put the code together but my browser is giving me nothing despite me having records in my MYSQL database. Would anyone mind scanning the code below to find any errors. I've checked and checked it, but apologies if it is a stray semi-colon. I have a feeling it may be discrepancies with my php tags as I have bastardised a tutorial as a template. If anyone has a rule for the correct opening and closing php that would be very welcome too. <html> <head></head> <body> <!-- standard page header --> <?php // includes include('conf.php'); include('functions.php'); // open database connection $connection = mysql_connect($host, $user, $pass) or die ('Unable to connect'); //select database mysql_select_db($db) or die ('Unable to select database'); // generate and excecute query // check for record ID if ( empty($_GET['wid']) ) { $wid=1; } else { $wid = $_GET['wid']; } $query = "SELECT word, category, tags, def, pub, author, submitted, amended, flagged FROM words WHERE wid = $wid"; $result = mysql_query($query) or die ("Error in query: $query. " . mysql_error()); // get resultset as object $row = mysql_fetch_object($result); //Create navigation links for top of page <?php echo <a href=\"record.php?wid=".$wid-1."\">Previous Entry</a><br />"; echo <a href=\"record.php?wid=".$wid+1."\">Next Entry</a><br />"; ?> // print details if ($row) { ?> <p> <font size="1"><?php echo ($row->word); ?></font> <p> <b><?php echo "Word ID: ".$record." (Primary Key)" ?> </b> <p> <font size="-1"><?php echo "Category: "($row->category); ?></font> <p> <font size="-1"><?php echo "Tags: "($row->tags); ?></font> <p> <font size="-1"><?php echo "Definition: "($row->def); ?></font> <p> <font size="-1"><?php echo "Published?: Feature to follow"; ?></font> <p> <font size="-1"><?php echo "Submitted by: "($row->author); ?></font> <p> <font size="-2"><?php echo "Date Submitted: "formatDate ($row->submitted); ?></font> <p> <font size="-1"><?php echo "Date Amended: Feature to Follow"; ?></font> <p> <font size="-1"><?php echo "Flagged: Feature to Follow"; ?></font> <p> <font size="-1"><?php echo "Votes: Feature to Follow"; ?></font> <p> <?php } else { ?> <p> <font size="-1">That record could not be located in the database.</font> <?php } // close database connection mysql_close($connection); ?> <!-- standard page footer --> </body> </html> Cheers, Gary Quote Link to comment https://forums.phpfreaks.com/topic/98400-assigning-variable-default/#findComment-503865 Share on other sites More sharing options...
zszucs Posted March 29, 2008 Share Posted March 29, 2008 This is where things went wrong. The open and close php tags weren't needed here. Also the echo statements here weren't right and the ones towards the bottom weren't right either. <?php echo <a href="record.php?wid=".$wid-1."\">Previous Entry</a><br />"; echo <a href="record.php?wid=".$wid+1."\">Next Entry</a><br />"; ?> This corrects the syntax errors. <html> <head></head> <body> <!-- standard page header --> <?php // includes include('conf.php'); include('functions.php'); // open database connection $connection = mysql_connect($host, $user, $pass) or die ('Unable to connect'); //select database mysql_select_db($db) or die ('Unable to select database'); // generate and excecute query // check for record ID if ( empty($_GET['wid']) ) { $wid=1; } else { $wid = $_GET['wid']; } $query = "SELECT word, category, tags, def, pub, author, submitted, amended, flagged FROM words WHERE wid = $wid"; $result = mysql_query($query) or die ("Error in query: $query. " . mysql_error()); // get resultset as object $row = mysql_fetch_object($result); //Create navigation links for top of page echo '<a href="record.php?wid='.$wid-1 .'">Previous Entry</a><br />'; echo '<a href="record.php?wid=' .$wid+1 .'">Next Entry</a><br />'; // print details if ($row) { ?> <p> <font size="1"><?php echo ($row->word); ?></font> <p> <b><?php echo "Word ID: ".$record." (Primary Key)" ?> </b> <p> <font size="-1"><?php echo "Category: " .$row->category; ?></font> <p> <font size="-1"><?php echo "Tags: " . $row->tags; ?></font> <p> <font size="-1"><?php echo "Definition: " . $row->def; ?></font> <p> <font size="-1"><?php echo "Published?: Feature to follow"; ?></font> <p> <font size="-1"><?php echo "Submitted by: " . $row->author; ?></font> <p> <font size="-2"><?php echo "Date Submitted: " . formatDate ($row->submitted); ?></font> <p> <font size="-1"><?php echo "Date Amended: Feature to Follow"; ?></font> <p> <font size="-1"><?php echo "Flagged: Feature to Follow"; ?></font> <p> <font size="-1"><?php echo "Votes: Feature to Follow"; ?></font> <p> <?php } else { ?> <p> <font size="-1">That record could not be located in the database.</font> <?php } // close database connection mysql_close($connection); ?> <!-- standard page footer --> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/98400-assigning-variable-default/#findComment-503872 Share on other sites More sharing options...
kingnutter Posted March 29, 2008 Author Share Posted March 29, 2008 Excellent. Thanks for that. I had to put brackets around the $wid increments / decrements for the link to show properly thus: echo '<a href="record.php?wid='.($wid-1) .'">Previous Entry</a><br />'; echo '<a href="record.php?wid=' .($wid+1) .'">Next Entry</a><br />'; Is this an issue with my text editor perhaps (into which I cut and pasted the code)? Quote Link to comment https://forums.phpfreaks.com/topic/98400-assigning-variable-default/#findComment-503957 Share on other sites More sharing options...
zszucs Posted March 29, 2008 Share Posted March 29, 2008 Is this an issue with my text editor perhaps (into which I cut and pasted the code)? It's an issue in that an editor could highlight thoses kinds of mistakes for you and make it easier to find. Zend studio will do that and probably others as well. I personally never had a problem with pasting code into an editor and it getting mangled. Quote Link to comment https://forums.phpfreaks.com/topic/98400-assigning-variable-default/#findComment-503993 Share on other sites More sharing options...
kingnutter Posted March 29, 2008 Author Share Posted March 29, 2008 Ah ok. I'm using HyperEdit (registered). Not top of its game methinks. Quote Link to comment https://forums.phpfreaks.com/topic/98400-assigning-variable-default/#findComment-504175 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.