Cerys Posted March 2, 2009 Share Posted March 2, 2009 Hi! I've got a page which displays various words and their translations based on their id numbers in a table. <?php if ($_GET['english']=='on'){ $result = mysql_query( "SELECT English FROM $table WHERE id = 24"); while ( $pr_row = mysql_fetch_row( $result ) ) { print ""; foreach ( $pr_row as $data ) print "\t $data"; print "\n"; } print "\n"; mysql_close ( $link ); } ?> I'm trying to find a way to make a link that will +1 to the 'WHERE id' number so that it displays those numbers? I'm really, really new to this stuff (been learning php since, oh, yesterday lunchtime) so I'd really appreciate any explainations you could give me! Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/147610-solved-how-can-i-create-a-link-that-will-change-where-id-1-by-1/ Share on other sites More sharing options...
camdenite Posted March 2, 2009 Share Posted March 2, 2009 I'm new to but this is an example of my blog edit page: if ($num > 0) { // If it ran OK, display the records. echo "<p>There are currntly $num Blog entries.</p>\n"; // Table header. echo '<table align="center" cellspacing="0" cellpadding="5"> <tr> <td align="left"><b>Date</b></td> <td align="left"><b>Heading</b></td> <td align="view"><b>View</b></td> <td align="left"><b>Edit</b></td> <td align="left"><b>Delete</b></td> </tr> '; // Fetch and print all the records. while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { echo '<tr> <td align="left"><a href="blog_view.php?id=' . $row['blog_id'] . '">' . $row['date2'] . '</a></td> <td align="left">' . $row['heading'] . '</td> <td align="left"><a href="blog_view.php?id=' . $row['blog_id'] . '">View</a></td> <td align="left"><a href="blog_edit4b.php?id=' . $row['blog_id'] . '">Edit</a></td> <td align="left"><a href="delete_blog.php?id=' . $row['blog_id'] . '">Delete</a></td> </tr> '; } Hope this helps. Quote Link to comment https://forums.phpfreaks.com/topic/147610-solved-how-can-i-create-a-link-that-will-change-where-id-1-by-1/#findComment-774926 Share on other sites More sharing options...
phant0m Posted March 2, 2009 Share Posted March 2, 2009 I'm not sure if this is what you meant, but from what I've got, this could be what you are looking for: <?php $id = (int) $_GET['id']; if($id == 0) $id = 24; //or any other default value that you want to set if($_GET['english']=='on'){ $result = mysql_query("SELECT English FROM $table WHERE id = $id LIMIT 1"); /*while($pr_row = mysql_fetch_row($result)){ // print ""; // why print an empty string? no use for this! foreach ( $pr_row as $data ) print "\t $data"; --->You don't need the foreach, because there is only one key in the array. print "\n"; }*/ // you don't need the while either, because there can only be one row returned at most if($data = mysql_fetch_row($result)){ $next_id = $id + 1; print($data[0].'<br /><a href="file.php?id='.$next_id.'">Next</a>'); //you might also want to escape the data with htmlentities() } else{ //no row has been found! } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/147610-solved-how-can-i-create-a-link-that-will-change-where-id-1-by-1/#findComment-774932 Share on other sites More sharing options...
Cerys Posted March 2, 2009 Author Share Posted March 2, 2009 Thanks phant0m, that works - but it raises another problem which I've been trying to work around fot the last half hour: if I click 'next', and then hit "english" to make english=on it removes the id number. I've tried changing it to be <a href="?id='$id'?english=on">English</a> but it won't work. I've tried google searches and searching the firums but haven't been able to find a solution. Could you help? Quote Link to comment https://forums.phpfreaks.com/topic/147610-solved-how-can-i-create-a-link-that-will-change-where-id-1-by-1/#findComment-774980 Share on other sites More sharing options...
premiso Posted March 2, 2009 Share Posted March 2, 2009 <a href="?id='$id'&english=on" To add more variables you use & the question mark should only be used for the initial one. Quote Link to comment https://forums.phpfreaks.com/topic/147610-solved-how-can-i-create-a-link-that-will-change-where-id-1-by-1/#findComment-774985 Share on other sites More sharing options...
phant0m Posted March 2, 2009 Share Posted March 2, 2009 oops - I forgot to add that to the url sorry thanks for the correction, premiso! Quote Link to comment https://forums.phpfreaks.com/topic/147610-solved-how-can-i-create-a-link-that-will-change-where-id-1-by-1/#findComment-774991 Share on other sites More sharing options...
Cerys Posted March 2, 2009 Author Share Posted March 2, 2009 Thank you both, that's exactly what I needed Life would be a lot easier if I had the ability to learn this stuff from books rather than trial and error Quote Link to comment https://forums.phpfreaks.com/topic/147610-solved-how-can-i-create-a-link-that-will-change-where-id-1-by-1/#findComment-774995 Share on other sites More sharing options...
phant0m Posted March 2, 2009 Share Posted March 2, 2009 you're welcome! well - learning from books doesn't have to be easier It's common to make such mistakes at the beginning. also, keep in mind that php.net is an excellent resource! Quote Link to comment https://forums.phpfreaks.com/topic/147610-solved-how-can-i-create-a-link-that-will-change-where-id-1-by-1/#findComment-774997 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.