Reputation: 513
I have a page that displays a row depending on the id given in the browser bar (page.php?id=1). I am trying to use forward and back buttons to display the corresponding next or previous row on the page. So essentially the prev and next buttons will just be links to page.php, with the id being the next in the list. It isn't a sequential list, so I can't just do id+1 etc.
Anyway, I have it working fine, but basically I want the forward and back links to only display as text if there isn't a page to go to. I've been trying to run code to display only text if the $backformat is empty, but it seems to hold "Resource id#(and a random number)". So I've tried some other ways to match "Resource" in the string but that hasn't worked
Any ideas?
<?php
$backresult = mysql_query("SELECT id FROM studies WHERE niche = '$niche' AND date < '$currentdate' ORDER BY date DESC LIMIT 1", $connection);
if (!$backresult) {
die("Database query failed: " . mysql_error());
}
$forwardresult = mysql_query("SELECT id FROM studies WHERE niche = '$niche' AND date > '$currentdate' ORDER BY date ASC LIMIT 1", $connection);
if (!$forwardresult) {
die("Database query failed: " . mysql_error());
}
?>
while ($row = mysql_fetch_array($forwardresult)) {
$forward = $row["id"];
$forwardformat = preg_replace('/\s+/','',$forward);
echo $forwardformat;
$pos = strpos($forwardformat,'source');
if($pos == false) {
// string needle NOT found in haystack
echo 'Exploring moves us <a href="casestudy.php?id=';
echo $forwardformat;
echo '">forward</a>';
}
else {
// string needle found in haystack
echo "forward";
}
//other code I've tried before
/* if (!empty($forwardformat)) {
echo 'Exploring moves us <a href="casestudy.php?id=';
echo $forwardformat;
echo '">forward</a>';
}
else {
echo "forward";
}*/
}
echo $backresult;
while ($row = mysql_fetch_array($backresult)) {
$back = $row["id"];
$backformat = preg_replace('/\s+/','',$back);
echo $backformat;
//$pos = strpos($backformat,'source');
//$match = preg_match("R",$backformat);
if (substr($backformat,0,1) == 'R') {
// string needle NOT found in haystack
echo ', studying we look <a href="casestudy.php?id=';
echo $backformat;
echo '">back</a>';
}
else {
// string needle found in haystack
echo "back";
}
//other code I've tried before
/*if (!empty($backformat)) {
echo ', studying we look <a href="casestudy.php?id=';
echo $backformat;
echo '">back</a>';
}
if ($backformat==false) {
echo "back";
} */
}
Upvotes: 0
Views: 708
Reputation:
You don't need while()
statements if you're only working with a single row. And yes, as posted above you test if a query returned any rows with mysql_num_rows($result)
, you can test if individual fields are empty with empty($result['fieldname'])
.
Upvotes: 1
Reputation: 20456
$backresult is PHP's connection to the result of your query. If you want to check that $backresult contains no rows, you need something like
if(mysql_num_rows($backresult)==0) {
//inactive link
} else {
//active link
}
Upvotes: 3