Wordpress: Fetching Data from Database $wpdb of Specific User - php

Hello I am working a project today. I want to print in front end the data of a specific user every time they login. When they login will get their user id and will fetch data from that specific user only.
I have this database. I want vendor_id 3 when this vendor login will display only values of specific vendor if no values will return "No Commission Data for this user"
Right now heres my wordpress wpdb code
<table border="1">
<tr>
<th>Vendor ID</th>
<th>Total Due</th>
<th>Time</th>
</tr>
<?php
global $wpdb;
$result = $wpdb->get_results ( "SELECT * FROM wp_pv_commission" );
$vendor = get_current_user_id();
foreach ( $result as $userdata) {
?>
<tr>
<td><?php echo $userdata->vendor_id;?></td>
<td><?php echo $userdata->total_due;?></td>
<td><?php echo $userdata->time;?></td>
</tr>
<?php
}
?>
</table>
And it returns the data of this. But what I want is only as per user like right now Im login as vendor id 3. It show all. What is the proper wordpress php conditioning code I need to use
Thank you for your answer.

Your code is right but your condition is wrong for selecting the data from database
when you don't apply where clause, MYSQL will always return all the records. so for that you need to provide where clause in your query
<table border="1">
<tr>
<th>Vendor ID</th>
<th>Total Due</th>
<th>Time</th>
</tr>
<?php
global $wpdb;
$vendor = get_current_user_id();
$result = $wpdb->get_results ( "SELECT * FROM wp_pv_commission where vendor_id=".$vendor );
foreach ( $result as $userdata) {
?>
<tr>
<td><?php echo $userdata->vendor_id;?></td>
<td><?php echo $userdata->total_due;?></td>
<td><?php echo $userdata->time;?></td>
</tr>
<?php
}
?>
</table>
How to use where clause in MySQL

Related

How do i access the child nodes INSIDE a child node in for displaying in php firebase?

After succeeding to input new table inside a key in php firebase, now i want to display the result.
I've tried using this
<table>
<tr>
<th>Course Code</th>
<th>Discriptive Title</th>
<th>Unit</th>
<th>Pre-Requisite</th>
<th>Grade</th>
</tr>
<?php
include('dbcon.php');
if(isset($_GET['id']))
{
$key_child = $_GET['id'];
$ref_table = 'User/'.$key_child.'/Grades/1st Year/1st Sem';
$row = $database->getReference($ref_table)->getChild($key_child)->getChild($key_child)->getValue();
if($row > 0 )
{
?>
<tr>
<input type="hidden" name = "id" value = "<?=$key_child;?>">
<td><?=$row['Course_Code']; ?></td>
<td><?=$row['Desc_title']; ?></td>
<td><?=$row['Unit']; ?></td>
<td><?=$row['Pre-Req']; ?></td>
<td><?=$row['Grade']; ?> </td>
</tr>
<?php
}
}
else
{
?>
<td colspan = "5"> No Record found </td>
<?php
}
?>
</table>
but the result was blank instead of returning "No Record found" as stated in the else code
I am trying to display the result from this grandchild table:
here are the href codes that will lead to the blank table if knowing it would help solve the problem
View
this link below will be the one that leads to the above table that doesnt work
samplegrades

how to add my database on a html table using session variables in php

I have a website that has multiple users that submit some requests, So each user will have his own Requests table that has been made. my issue is I'm trying to show the requests for every user in a table by using the session variables to get the name of the user and match it with the request table but it dose not work.
$userin= $_SESSION['username'];
$req = "SELECT * FROM request_table where r_status='pending' AND requester = 'max';";
$request_table = $conn->query($req);
it works if I out ( Max ) only but when I put it as a session variable or as $userin it dose not work and I get this error :
Notice: Trying to get property 'num_rows' of non-object in C:\xampp\htdocs\website\b_table.php on line 54.
Below is my code for the table :
<body>
<h1 align="center">Table</h1>
<table border="5" align="center" style="line-height:20px;">
<tr>
<th>Request ID</th>
<th>Requester name</th>
<th>Customer name</th>
<th>Description</th>
<th>Submition Date</th>
<th>Request Status</th>
<th>link</th>
</tr>
<?php
//Fetch Data form database
if($request_table->num_rows > 0){
while($view_request = $request_table->fetch_assoc()){
?>
<tr>
<td><?php echo $view_request['request_id']; ?></td>
<td><?php echo $view_request['requester']; ?></td>
<td><?php echo $view_request['customer_name']; ?></td>
<td><?php echo "Site A is:". $view_request['site_a']. ",<br> Site B is: ".$view_request['site_b']. ",<br>NO. Of links =:".$view_request['no_oflinks']; ?></td>
<td><?php echo $view_request['sub_date']; ?></td>
<td><?php echo $view_request['request_status']; ?></td>
<form action="view_request.php" method="GET"><td><button type="vieww" name="vieww" value= <?php echo $view_request['request_id']; ?>> View </button></td></form>
</tr>
<?php
}
}
else
{
?>
<tr>
<th colspan="7">No Data availabe</th>
</tr>
<?php
}
$conn->close();
?>
</table>
``````````````````````````````````````````````````````````````````````````````````````````
[enter image description here][1]
[1]: https://i.stack.imgur.com/pOHOX.jpg
you can do some debugging and testing.
like check if session variable is set or not like :
if(isset($_SESSION['username'])){
$userin= $_SESSION['username'];
}
else{
die("Session user name wasn't set");
}
and for the query
$req = "SELECT * FROM request_table where r_status='pending' AND requester = '$userin';"
for more debugging and testing , you can use vardump to check if data is present or not like : vardump($request_table);
hope it helps you in some way...
hope it helps.

Send Variables to Another Page from a Table Created by PHP [duplicate]

This question already has answers here:
How to fix "Headers already sent" error in PHP
(11 answers)
Closed 3 years ago.
I have created on table with a PHP while loop right from the phpmyadmin database fine on page 1 (cpu.php). However, I am not able to send just the name variable ( $row['name'] ) from one selected row to another page, page 2 (cpumore.php). I have tried using sessions and the closest I have come is the code given that outputs the name of my last listed cpu from the cpu.php page to the cpumore.php page. I hope someone can help me pass variables from a selected row to the other table on page 2 (cpumore.php). Thank you so much!
PAGE 1 CPU.PHP
<table id="myTable">
<thead>
<tr>
<th onclick="sortTable(0)">Name</th>
<th onclick="sortTable(1)">Socket</th>
<th onclick="sortTable(2)">Speed</th>
<th>Price</th>
<th></th>
</tr>
</thead>
<?php
session_start();
if(isset($_POST['search'])) {
$valueToSearch = $_POST['valueToSearch'];
// search in all table columns
// using concat mysql function
$query = "SELECT name, socket, speed, price FROM `cpu` WHERE
CONCAT(`name`, `socket`, `speed`, `price`) LIKE
'%".$valueToSearch."%'";
$search_result = filterTable($query);
} else {
$query = "SELECT name, socket, speed, price FROM `cpu`";
$search_result = filterTable($query);
}
// function to connect and execute the query
function filterTable($query)
{
$connect = mysqli_connect("localhost", "root", "", "brodbuilds");
$filter_Result = mysqli_query($connect, $query);
return $filter_Result;
}
?>
<?php while($row = mysqli_fetch_array($search_result)):?>
<tr>
<form action="cpumore.php" method="post">
<td><?php echo $row['name']; $_SESSION['cpuName']=$row['name'];?</td>
<td><?php echo $row['socket'];?></td>
<td><?php echo $row['speed'];?></td>
<td><?php echo $row['price'];?></td>
<td><input type="submit" name=submit id="cpuname" value="Add"/>
</div</td>
</form>
</tr>
<?php endwhile;?>
</table>
PAGE 2 CPUMORE.PHP
<table id="myTable">
<thead>
<tr>
<th onclick="sortTable(0)">Name</th>
<th></th>
</tr>
</thead>
<tr>
<td> <?php session_start(); echo $_SESSION['cpuName'];?> </td>
</tr>
</table>
I would like to be able to click on a button that send the name field from one row to another page.
You missed '>' Try now
<td><?php echo $row['name']; $_SESSION['cpuName']=$row['name'];?></td>
I'm not too sure if I get the question, the best is to have a session manager, or alternatively on more add a variable as page?varname=value on page to use:
$Var = $_Get['varname'];

PHP and Mysql while loop

I have a mysql table with orders. I am trying to loop through the orders table and select individual client orders according to their user id and display the orders on their client accounts. However, my code below just prints the first row and repeats it endlessly jaming my browser every time.
what is wrong with this and how to i solve it
<?php
$records = $conn->prepare('SELECT * FROM orders WHERE user_id= :id');
$records-> bindParam(':id', $_SESSION['user_id']);
$records->execute();
$results=$records->fetch(PDO::FETCH_ASSOC);
$i=0;
while($i<=$results):
$i++;
?>
<h3>Your Orders</h3>
<table >
<tr >
<th>Order Number</th><th>Academic Level</th><th>Order Details</th>Manage Order</th>
</tr>
<tr>
<td>#SJ<?=$results['id']; ?> </td><td><?=$results['academic_level']; ?></td><td ><?=$results['details']; ?></td>
</tr>
</table>
<?php
endwhile;
?>
Remove all $i related code. Just move your fetch statement to while condition, like the following:
while( $results=$records->fetch(PDO::FETCH_ASSOC))
You need to include the html code in the while loop because you want to display all of the orders.
I'm using this method, try this out.
Start while in first php section
<?php
$username = $_SESSION['username'];
$sql = $db->prepare("SELECT * FROM tableName WHERE username = '$username' ");
$sql->execute();
while($results=$sql->fetch(PDO::FETCH_ASSOC)){
?>
After that the html section coming:
<h3>Your Orders</h3>
<table >
<tr >
<th>Order Number</th><th>Academic Level</th><th>Order Details</th>Manage Order</th>
</tr>
<tr>
<td><?php echo $results['id']; ?> </td><td><?php echo $results['academic_level']; ?></td><td ><?php echo $results['details']; ?></td>
</tr>
</table>
and here the ending
<?php
}
?>

Created a table with 4 columns and 3 rows. How do I set up a php script to pull each row by id to generate 3 tr from the dababase?

The table I am looking to pull from my database is com/bzkItsK. It currently pulls the first row in the database but I am unsure about how to set up a script that will pull all the rows (currently 4) by their id to the webpage.
Here is the html as I have set it:
<table class="table table-striped">
<thead>
<tr>
<th>user id</th>
<th>First Name</th>
<th>Last Name</th>
<th>Department</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row"><?php echo $rows[$user_id];?></th>
<td><?php echo $rows[$first_name];?></td>
<td><?php echo $rows[$last_name];?></td>
<td><?php echo $rows[$dept];?></td>
</tr>
</tbody>
</table>
mysql_query is as such.
mysql_connect("localhost","?","?");
mysql_select_db("?");
$sql = mysql_query("SELECT * FROM users ORDER BY id ASC");
$id = 'id';
$user_id = 'user_id';
$first_name = 'first_name';
$last_name = 'last_name';
$dept = 'dept';
$rows = mysql_fetch_assoc($sql);
?>
I am trying to pull all 4 rows by id to be auto generated by a single table script.
you must iterate over your results.
$rows = mysql_fetch_assoc($sql);
will only fetch the first entry.
You have do something like this:
<?php while($rows = mysql_fetch_assoc($sql)): ?>
<tr>
<th scope="row"><?php echo $rows[$user_id];?></th>
<td><?php echo $rows[$first_name];?></td>
<td><?php echo $rows[$last_name];?></td>
<td><?php echo $rows[$dept];?></td>
</tr>
<?php endwhile; ?>
EDIT:
And please do not use mysql, use mysqli instead.

Categories