Jump to content

Simple PHP help


timprice
Go to solution Solved by timprice,

Recommended Posts

Hi all,

I am new to php, and just using some help guides to try and build a basic system.

What I would like is an output page to display a table with selected data in (that I can do)

I would how ever like the the table rows to be links to another page which contains more information of which ever row I clicked.

eg:/

output.php
 Job Ref | Client | Site | Description   <------click this row of data to take you to the next page.

output2.php
Start Date | Duration | Machines | Technicians <------- This page only shows the data taken from the row above.

This is my code for the 1st page (output.php) Basic Code

 

<html>
	<meta http-equiv="refresh" content="120">
</html>

<?php

$con=mysqli_connect("localhost","root","","database");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

$result = mysqli_query($con,"SELECT * FROM table");

echo "<table border='1'>
<tr>
<th>Job Ref</th>
<th>Client</th>
<th>Site</th>
<th>Job Description</th>
</tr>";

while($row = mysqli_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['jobref'] . "</td>";
  echo "<td>" . $row['client'] . "</td>";
  echo "<td>" . $row['site'] . "</td>";
  echo "<td>" . $row['Job_description'] . "</td>";
  echo "</tr>";
  }
echo "</table>";

mysqli_close($con);
?> 

Thanks for any help guys.

Edited by timprice
Link to comment
Share on other sites

I have been trying this but I dont think its is carrying over the row ID to the seconds page.

 

while($row = mysqli_fetch_array($result))
  {
  echo "<tr>";
  echo "<td><a href=\"secondpage.php?id={$row['id']}\">{$row['jobref'] }</a> </td>";
  echo "<td><a href=\"secondpage.php?id={$row['id']}\">{$row['client'] }</a> </td>";
  echo "<td>" . $row['site'] . "</td>";
  echo "<td>" . $row['Job_description'] . "</td>";

  echo "</tr>";
  }
echo "</table>";

mysqli_close($con);
?> 

Secondpage

<?php

$con=mysqli_connect("localhost","root","","database");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

$rowId=$_GET['id'];
  
$result = mysqli_query($con,"SELECT * FROM table WHERE id=$rowId");

echo "<table border='1'>
<tr>
<th>Job Ref</th>
<th>Client</th>
<th>Site</th>
<th>Job Description</th>
</tr>";

while($row = mysqli_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['jobref'] . "</td>";
  echo "<td>" . $row['client'] . "</td>";
  echo "<td>" . $row['site'] . "</td>";
  echo "<td>" . $row['Job_description'] . "</td>";
  echo "</tr>";
  }
echo "</table>";

mysqli_close($con);
?> 
Link to comment
Share on other sites

 

 

  1. echo "<td><a href=\"secondpage.php?id={$row['id']}\">{$row['jobref'] }</a> </td>";
  2. echo "<td><a href=\"secondpage.php?id={$row['id']}\">{$row['client'] }</a> </td>";

That should pass the rows id to secondpage provided you do have a column called id in your database, which holds the records id.

 

The code used in secondpage.php should work, however I'd use the following when getting the id

$rowId=intval($_GET['id']);
Link to comment
Share on other sites

Arrrrh, right yeah it was failing becuase 'id' was in caps in the database.
Ok so thats working, but it makes each of those as hyperlinks to a new page which while in essence is fine, what I would like is for the whole row to be a link.
Im not sure how to do that.

Can I put that code say here:

 

    while($row = mysqli_fetch_array($result))
    {
    echo "<tr><a href=\"secondpage.php?id={$row['id']}\">";
    echo "<td>" . {$row['jobref'] }. "</td>";
    echo "<td>" . {$row['client'] }. "</td>";
    echo "<td>" . $row['site'] . "</td>";
    echo "<td>" . $row['Job_description'] . "</td>";
     
    echo "</a></tr>";
    }
    echo "</table>";
     
    mysqli_close($con);
    ?> 

To make the whole row a link not just the text?

Edited by timprice
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.