I have created a notification list in my html page using php.
I need to load relevant data when clicking on the list item.(ex: when clicking on "[customer_name] needs to register" , it should load all the other data under that customer name, in a separate page)I have tried with following code but didn't get desired outcome.
Could you please help me to solve the problem.
<?php
$dbc = mysqli_connect('localhost','root','','dashboard') or die(mysqli_connect_error(''));
function notification() {
global $dbc;
$query = "SELECT `customer_name` FROM `customer` WHERE `confirmation`IS NULL LIMIT 0, 30 ";
$result = mysqli_query($dbc,$query);
global $row;
while($row=mysqli_fetch_array($result))
{
echo"<a href='CusRegReport.php'>"."<i class='fa fa-users text-red'>"." ".$row['customer_name']." "."needs to register"."</i>"."</a>";
global $x;
$x = $row['customer_name'];
}
}
function details(){
global $x;
$sql = "SELECT * FROM `customer` WHERE `customer_name` = '$x'";
$r = mysqli_query($dbc,$sql);
while($line=mysqli_fetch_array($r))
{
$d = "<br/>".$line['customer_name']."<br/>".$line['ad_line_one']."</a>";
}
}
?>
I have just polished the code little bit.
index.php
: Here you will show the notifications.
<?php
$dbc = mysqli_connect('localhost','root','','dashboard') or die(mysqli_connect_error(''));
function notification(){
global $dbc;
$query = "SELECT `customer_id`, `customer_name` FROM `customer` WHERE `confirmation`IS NULL LIMIT 0, 30 ";
$result = mysqli_query($dbc,$query);
while($row=mysqli_fetch_array($result))
{
echo "<a href='CusRegReport.php?id=" . $row['customer_id'] . "'><i class='fa fa-users text-red'> " . $row['customer_name'] . " needs to register</i></a>";
}
}
?>
<!doctype html>
<html>
<body>
<?php notification(); ?>
</body>
</html>
CusRegReport.php
: Here you will show the details of the Customer with the particular id.
<?php
$dbc = mysqli_connect('localhost','root','','dashboard') or die(mysqli_connect_error(''));
function details(){
global $dbc; // we need this in all our functions. Then only we will be able to do the db operations within the function
$id = $_GET['id']; // we are using the id that was passed in the URL
$id = $mysqli->real_escape_string( $id ); // clean the id that the user passed via url, so that we could use it in our SQL query. Otherwise it is unsafe to use directly because it can lead to SQL injections
$sql = "SELECT * FROM `customer` WHERE `customer_id` = '$id'";
$r = mysqli_query($dbc,$sql);
while($line=mysqli_fetch_array($r))
{
echo "<br/>" . $line['customer_name'] . "<br/>" . $line['ad_line_one'] . "</a>";
}
}
?>
<!doctype html>
<html>
<body>
<?php details(); ?>
</body>
</html>
Things to remember:
You have declared the mysqli object $dbc
at the very top of the page. So if you want to use that object to access the db, from inside a function, you need to use the global
keyword like this: global $dbc;
Avoid excessive use of the keyword global
. You were using it to declare each variable! That's not needed.
When you echo the <a>
links to the customer details page, include the customer_id
in the url after a ?
. The data we pass like that, will be available in the $_GET
array at the resulting page.
Always try to use an integer value to identify the customer. Because more than one customer can have the same name! Since you are already having the details of the customer in your db, I believe you have used a PRIMARY KEY
for that table, which might be customer_id
, I guess. So use that.
Avoid unwanted concatenations. It could give confusions to you later. In your first echo
line, you have used lots of concatenations even though you were concatenating just the strings.
Try to escape the data that you accept from the user, before using it in your SQL queries. Because the data that the user provides may cause sql injection attacks. So always clean the user input.
These are just quick basic things for you.
Hope it will help. NOTE: I only polished your code a little bit. No testing or anything done.
I can see two approaches for you here:
Customer Class
What I recommend in this case is the use of the $_SESSION
global array, so you can pass data from one page to another. The overal proccess could be something like this:
function details()
seems to be the proper place.The code should be, simply put:
//Customer class
class Customer
{
//All data and methods for the class to work
}
//...
function details()
{
global $x;
$currentCustomer = new Customer();
$sql = "SELECT * FROM `customer` WHERE `customer_name` = '$x'";
$r = mysqli_query($dbc,$sql);
while($line=mysqli_fetch_array($r))
{
$currentCustomer->name = $line["name"];
$currentCustomer->age = $line["age"];
//etc...
}
$_SESSION["CurrCustomer"] = $currentCustomer;
}
//... In some other page
$customer = (Customer)$_SESSION["CurrCustomer"];
echo "<br/>".$customer->name."<br/>".$customer->age." years</a>";
Using $_GET
This option should be more simple. Let's say you have two pages, one where you show the list of clients you said (index.php
), and one where you show details of the customer (customer.php
). With the $_GET parameters, you can pass easily data from one page to the other, and the second can use them to do something.
Simply put:
index.php
function notification()
{
//(Just as you have it)
}
//In case you want to move the user automatically to the other page:
header("Location: customer.php?n=".$x);
//OR, in case you want to use a link there
echo "<a href='customer.php?n=".$x."'>Go there!</a>";
customer.php
//Something very similar to your version of details()
if (!isset($_GET["n"]))
header("Location: index.php");
else
{
$customerName = $_GET["n"];
$sql = "SELECT * FROM `customer` WHERE `customer_name` = '$customerName'";
$r = mysqli_query($dbc,$sql);
while($line=mysqli_fetch_array($r))
{
$d = "<br/>".$line['customer_name']."<br/>".$line['ad_line_one']."</a>";
}
}
I hope this helps you. Let me know if you have doubts or it isn't clear. Good luck!