Hi guys,

Ok, so I'm using the code below to display the fields from a table on my webpage. I used some code to paginate the results as there's a lot of records, however I can't seem to get it working properly, and I'm going crazy trying to work out why.

As far as i can tell, I've defined everything properly, I've highlighted the line that's causing me issues in bold. It's where I call the results up, the last part "$website" should call up the result from the URL field in the db as a clickable url.

Can anyone help and show me where I'm going wrong please?

Thanks :)

<?php
$conn = mysql_connect(HOST,USER,PASS);
$db = mysql_select_db("db303278079") or die( "Unable to select database");
$sql = "SELECT COUNT(*) FROM ".RESTAURANTS." where area='manchester'";
$result = mysql_query($sql, $conn) or die ("unable to select db");
$r = mysql_fetch_row($result);
$numrows = $r[0];
$rowsperpage = 10;
$totalpages = ceil($numrows / $rowsperpage);
if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) {
   $currentpage = (int) $_GET['currentpage'];
} else {
   $currentpage = 1;
}
if ($currentpage > $totalpages) {
   $currentpage = $totalpages;
}
if ($currentpage < 1) {
    $currentpage = 1;
}
$offset = ($currentpage - 1) * $rowsperpage;
$sql = "select * from ".IWARE_DOCS2." WHERE area='manchester' order by link_text LIMIT $offset, $rowsperpage";
$result = mysql_query($sql, $conn) or die ("unable to select db2");
$rest_name=mysql_result($result,$i,"link_text");
$address=mysql_result($result,$i,"address");
$telephone=mysql_result($result,$i,"telephone");
$website=mysql_result($result,$i,"url");
$cuisine_type=mysql_result($result,$i,"cuisine_type");
$website=(empty($website))?'':", <strong><A HREF=$website>Website</a></strong>";
while ($list = mysql_fetch_assoc($result)) {
[B]echo "<strong>" . $list['link_text'] . "</strong>" . " | " . $list['address'] . " | " . $list['telephone'] . $list['$website'] . "<br /><br>";[/B]
}
$range = 5;
if ($currentpage > 1) {
   echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=1'><<</a> ";
   $prevpage = $currentpage - 1;
   echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage'><</a> ";
}
for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) {
   if (($x > 0) && ($x <= $totalpages)) {
      if ($x == $currentpage) {
         echo " [<b>$x</b>] ";
      } else {
         echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$x'>$x</a> ";
      }
   }
}
   
if ($currentpage != $totalpages) {
   $nextpage = $currentpage + 1;
   echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$nextpage'>></a> ";
   echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$totalpages'>>></a> ";
}
?>

#1: Change your post to have php style code=php


#2: whats the error or whats not working correctly. we don't have a copy of your data

sorry, not sure how I messed that up, but can't seem to edit post so I'll try again....

the problem lies on line 31, in that all data for each record displays properly, up until the '$website' variable. It should display the data (a URL) stored in the corresponding field on the record and make it clickable, but it's not displaying anything as-is.

I thought it might be my syntax, so removed the $list command and had it as . $website . - this only displayed the url field from the first record repeatedly against every record.... when what should happen is that it should only display if there is an entry there, and if that field is blank for that record it should display nothing....

does that make sense?

<?php
$conn = mysql_connect(HOST,USER,PASS);
$db = mysql_select_db("db303278079") or die( "Unable to select database");
$sql = "SELECT COUNT(*) FROM ".RESTAURANTS." where area='manchester'";
$result = mysql_query($sql, $conn) or die ("unable to select db");
$r = mysql_fetch_row($result);
$numrows = $r[0];
$rowsperpage = 10;
$totalpages = ceil($numrows / $rowsperpage);
if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) {
   $currentpage = (int) $_GET['currentpage'];
} else {
   $currentpage = 1;
}
if ($currentpage > $totalpages) {
   $currentpage = $totalpages;
}
if ($currentpage < 1) {
    $currentpage = 1;
}
$offset = ($currentpage - 1) * $rowsperpage;
$sql = "select * from ".IWARE_DOCS2." WHERE area='manchester' order by link_text LIMIT $offset, $rowsperpage";
$result = mysql_query($sql, $conn) or die ("unable to select db2");
$rest_name=mysql_result($result,$i,"link_text");
$address=mysql_result($result,$i,"address");
$telephone=mysql_result($result,$i,"telephone");
$website=mysql_result($result,$i,"url");
$cuisine_type=mysql_result($result,$i,"cuisine_type");
$website=(empty($website))?'':", <strong><A HREF=$website>Website</a></strong>";
while ($list = mysql_fetch_assoc($result)) {
echo "<strong>" . $list['link_text'] . "</strong>" . " | " . $list['address'] . " | " . $list['telephone'] . $list['$website'] . "<br /><br>";
}
$range = 5;
if ($currentpage > 1) {
   echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=1'><<</a> ";
   $prevpage = $currentpage - 1;
   echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage'><</a> ";
}
for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) {
   if (($x > 0) && ($x <= $totalpages)) {
      if ($x == $currentpage) {
         echo " [<b>$x</b>] ";
      } else {
         echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$x'>$x</a> ";
      }
   }
}
   
if ($currentpage != $totalpages) {
   $nextpage = $currentpage + 1;
   echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$nextpage'>></a> ";
   echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$totalpages'>>></a> ";
}
?>

If you want to display the url or nothing, i'd use a ternary operation

if your url has the HTML in it already use

$theurl = strlen($list['$website'])>0?$list['$website']:'';
echo "<strong>" . $list['link_text'] . "</strong>" . " | " . $list['address'] . " | " . $list['telephone'] . $theurl. "<br /><br>";

if your url is just the url use

$theurl = strlen($list['$website'])>0?"<a href = \"".$list['$website'].">".$list['$website']."</a>":'';
echo "<strong>" . $list['link_text'] . "</strong>" . " | " . $list['address'] . " | " . $list['telephone'] . $theurl. "<br /><br>";
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.