0

I am a newbie here and for php too. I am not new to stackoverflow though :)

I am creating a simple reporting system and I want the menues and pages to be generated from the database.

I saw this video on YouTube and managed to create a menu with the following code.

I have a database table called Reports and columns called rep_id, rep_date, rep_ledit_date, rep_by, department, position, report, and rep_to.

So based on the above method, I created a menu using this code.

<?php 

require ("includes/db.php");

mysqli_select_db($con, '$db_name');

$sql = "SELECT * FROM reports";
$result = mysqli_query($con, $sql) or die (mysqli_error($con));
$represult=mysqli_fetch_assoc($result);
$rep_by=$represult['rep_by'];
$report=$represult['report']; 

?>  
<li> Menu
<ul>
<?php do 
       {
?>
<li><a href="reports.php?rep_id=<?php echo $represult['rep_id']; ?>"> <?php echo $represult['rep_by'] . " (" .   $represult['rep_date'] . ")"; ?></a></li>

<?php 
     } while ($represult=mysqli_fetch_assoc($result));
    //$represult['rep_by'];
    //$represult['report'];
    //$represult['report'] ;
?>
</ul>
</li>    

So I created a page called reports.php to see the details of the content in the database. What I wanted was to see the following rep_by (rep_date) as a heading and report as a content.

I might want to use other columns in the content too. So what kind of code the menu and reports.php should have to achieve what I want. What I did was the following and it only outputs the first row when all the menu links are clicked.

 require ("includes/db.php");

 mysqli_select_db($con, '$db_name');

 $sql= "SELECT * FROM reports";
 $result = mysqli_query($con, $sql) or die (mysqli_error($con));
 $represult=mysqli_fetch_assoc($result); 

 <h1> <?php echo $represult['rep_by'] . " (" . $represult['rep_date'] . ")"; ?></a></h1>
 <?php echo $represult['report']; ?>
wondim
  • 697
  • 15
  • 29
  • It looks like you provided the PHP code for menu.php. Are you asking for an example of the PHP code for reports.php? – bloodyKnuckles May 25 '14 at 22:42
  • I provided an answer that cleared up a major issue I saw in your code—`$represult=mysqli_fetch_assoc($result)` being used twice—but unclear what the question is? Was this code not working? Is my answer now a solution? Flagged as “Unclear” unless you can edit/amend the question to clarify. – Giacomo1968 May 25 '14 at 22:45
  • @bloodyKnuckles, I modified it now. The error was made when posting on this forum. The problem is still there on reports.php – wondim May 26 '14 at 04:13
  • The explanation is much clearer. You provided the PHP code for reports.php (plural). However your "
  • – bloodyKnuckles May 26 '14 at 12:12
  • @bloodyKnuckles, good observation. That is again my bad in writing it on this forum. I will fix it. My problem is, the pages doesn't show their corresponding report, rather just the first row from the database. – wondim May 26 '14 at 12:20
  • Yes, the "WHERE rep_id = ?" part of the query was missing. See the provided report.php example. – bloodyKnuckles May 26 '14 at 12:54