This is my first posting in SO, my apologize if I opened an existing question. As I couldn't find the result in Google. Sorry to said but I'm still fresh in PHP PDO and in learning stage.
Back to my question, currently I'm building a customer visit logs from my wife but I'm stuck with the result. I have two table which one stores the customer information and another table store the visit details. I uploaded the test table at here: SQL Fiddle
And below is my current coding and I'm using PHP PDO while
<?php
require_once 'dbconnect.php';
$p_id = $_GET['name'];
try {
$conn = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);
$sql = "SELECT customer.fname, customer.lname, customer.gender, services.treatment, services.date
FROM customer LEFT JOIN services
ON customer.id = services.customer_id
WHERE customer.slug LIKE :id";
$q = $conn->prepare($sql);
$q->execute(array('id' => $p_id));
$q->setFetchMode(PDO::FETCH_ASSOC);
} catch (PDOException $pe) {
die("Could not connect to the database $dbname :" . $pe->getMessage());
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Test</title>
</head>
<body>
<div id="container">
<h1>Customer Record</h1>
Name: <br />
Gender: <br />
<table class="table table-bordered table-condensed">
<thead>
<tr>
<th>Customer Name</th>
<th>Customer Gender</th>
<th>Treatment</th>
<th>Date</th>
</tr>
</thead>
<tbody>
<?php while ($r = $q->fetch()): ?>
<tr>
<td><?php echo htmlspecialchars($r['fname']), ' ', htmlspecialchars($r['lname'])?></td>
<td><?php echo htmlspecialchars($r['gender']); ?></td>
<td><?php echo htmlspecialchars($r['treatment']); ?></td>
<td><?php echo htmlspecialchars($r['date']); ?></td>
</tr>
<?php endwhile; ?>
</tbody>
</table>
Seach Again
</body>
</div>
</html>
And I achieve as what SQL Fiddle result, but what I wanted is, the name and gender is not keep repeating.
I attached together with the screenshot:
Screenshot
What I want is as per the screenshot image, Name: John Doe and Gender: Male, should be on top and not keep on repeating while the table below show all the visit details. I tried to modified the code but it seems it don't really work out.
Please advise me as I'm really out of idea how to achieve what I want.
Thank you so much.
Since you do a LEFT JOIN in your SQL query, you know ahead of time that all of the fname, lname and gender values returned by $q->fetch() are going to be for the same customer.slug, right? So you can count on that.
My suggestion would be to instead use the fetchAll() function to get an array of all records for customer.slug, and then render that in your view. For example (haven't tested this) you could add the following after $q->setFetchMode(PDO::FETCH_ASSOC); ...
$cs = $q->fetchAll(); // customer services join
Then, in your <html> view, you could do something like the following:
<h1>Customer Record</h1>
Name: <?php echo htmlspecialchars($cs[0]['fname'].' '.$cs[0]['lname']); ?> <br />
Gender: <?php echo htmlspecialchars($cs[0]['gender']); ?> <br />
<table>
<thead>
<tr>
<th>Treatment</th>
<th>Date</th>
</tr>
</thead>
<tbody>
<?php foreach($cs as $r): ?>
<tr>
<td><?php echo htmlspecialchars($r['treatment']); ?></td>
<td><?php echo htmlspecialchars($r['date']); ?></td>
</tr>
<?php endforeach; ?>
</tbody>
Of course, it might also be a good idea to check to see that any records were returned by your query and display a "not found" message if not. After all, $cs[0] might be empty, giving you a PHP error.
Related
I'm having some trouble generating output from an sql query via php. When I execute the query "SELECT * from projectdb WHERE name = 'Crys' or ID = 14142" on phpmyadmin it returns valid results, but attempting to do so by passing a post value yields an empty table. See code below:
<html>
<title>Search result</title>
<body>
<table border="1px">
<tr>
<td>name</td>
<td>ID</td>
<td>position</td>
<td>job scope</td>
<td>contact_no</td>
<td>days_off</td>
<td>wages</td>
</tr>
<?php
if (isset($_POST['value']))
{
$ID=$_POST['staff ID'];
$name=$_POST['staff name'];
$admincon =mysqli_connect("localhost","root","","projectdb");
/* Query I need to execute and print in table*/
$sqlsrch2 =mysqli_query($admincon, "select * from staff where name='".$name."' or ID='".$ID."'");
while($result=mysqli_fetch_assoc($sqlsrch2)){
?>
/* table where results needs to be printed */
<tr>
<td><?php echo $result['name'];?></td>
<td><?php echo $result['ID'];?></td>
<td><?php echo $result['position'];?></td>
<td><?php echo $result['job_scope'];?></td>
<td><?php echo $result['contact_no'];?></td>
<td><?php echo $result['days_off'];?></td>
<td><?php echo $result['wages'];?></td>
</tr>
<?php
}
}
?>
</table>
</body>
</html>
A few points to note:
I'm just a beginner in PHP and SQL; I'm just looking for a simple answer.
Security is not at all a concern here; this is just a rough demo.
If this is marked as duplicate, do help redirect me to a link where I can get the solution. Thanks!
I am making posts of computer builds. I have a completely separate MYSQL database that I track computer components, assigned to each build. For instance, BuildID = 4 will have "Asus Rampage VI Extreme Motherboard x299", Core i9 7980xe Extreme LGA 2066" as values that come from the following query:
"SELECT BCats.Cat, BParts.ItemName, BParts.ItemSKU, BParts.ItemURL, BParts.ItemPrice, BParts.Qty, BParts.ItemPrice*BParts.Qty AS Price
FROM BParts
INNER JOIN BCats ON BCats.CatNo = BParts.ItemCat
WHERE BParts.BuildNo = 4 AND BParts.SystemNo = 1
ORDER BY BCats.CatNo ASC"
I need to take "BParts.BuildNo = 4" and turn it into a variable, such as "BParts.BuildNo = $buildNo". Then, for each customer that builds one of my desks, I can a photo gallery and just insert the shortcode [build] posts the table for all of the parts belonging to build number 4 (see it work at https://badvolf.com/en/declassified-systems-build/
Forgive me, but I am stumped at how to do it. I have searched everywhere, but nothing seems to fit what I am looking for, but I know that it can be done somehow. Here is what I have so far, and it generates the table that you see on the above link:
function wp_build_shortcode(){
$query = "SELECT BCats.Cat, BParts.ItemName, BParts.ItemSKU, BParts.ItemURL, BParts.ItemPrice, BParts.Qty, BParts.ItemPrice*BParts.Qty AS Price
FROM BParts
INNER JOIN BCats ON BCats.CatNo = BParts.ItemCat
WHERE BParts.BuildNo = 4 AND BParts.SystemNo = 1
ORDER BY BCats.CatNo ASC";
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Build Name</title>
</style>
</head>
<body>
<table id="example" class="display" cellspacing="0" width="100%">
<col width="15%">
<col width="85%">
<col width="20%">
<thead>
<tr>
<th></th>
<th>Item</th>
<th>Qty</th>
<th>Total Price</th>
</tr>
</thead>
<tbody>
<?php
if ($result = mysqli_query($GLOBALS['link'], $query)) {
while ($row = $result->fetch_assoc()) { ?>
<tr>
<td><?php echo $row["Cat"]; ?></td>
<td><?php echo $row["ItemName"]; ?></td>
<td><?php echo $row["Qty"]; ?></td>
<td><?php echo $row["Price"]; ?></td>
</tr><?php }
} ?>
</tbody>
</table>
<?php
}
add_shortcode('build', 'wp_build_shortcode');
?>
So, how can I do something like [build="4"] and pass that value on to the query? Thank you in advance!
You need to give your function at least one parameter. For Example wp_build_shortcode($attributes). $attributes will be an array with the parsed shortcode parameters. In your case ['build' => '4']. Then you can assign it to any local variable:$buildNo = issest($attributes['build']) ?
intval($attributes['build']) : 0;`. You can find more information here.
In WP shortcodes are accessible for very low privileged users (subscribers). Therefore you should treat the parameters as untrusted user input, and protect yourself from SQL injections and other exploits. In your case the intval in the above code should be enough, but prepared statements are a better practice.
How do I make a member list with PHP and MySQL?
I have user accounts, login and that stuff. How do I make a page that shows all the members?
I can provide code!
NOTE: Your question is not clear. You have not included any code or query snippet. I will try my best to answer your question.
As per my knowledge I think your users are nothing but the members. Just write a query to pull all the users from the users table and display.
NOTE : I will use mysqli_* function without any escaping please help yourself.
<?php
include_once 'db_connect.php'; /Line to include the database connection file, which had $link as resource */
$membersQuery = mysqli_query($link, "SELECT * FROM users");
$members = array();
if(mysqli_num_rows($membersQuery) > 0){
while($row = mysqli_fetch_assoc($membersQuery)){
$members[] = $row; //Get all the members row by row and store in $members array
}
}
?>
<table>
<thead>
<tr>
<th>Firstname</th>
</tr>
</thead>
<tbody>
<?
if(count($members) > 0){
foreach($members as $member){
<tr>
<td><?php echo $member['firstname']; ?></td>
</tr>
<?php
}
}
?>
</tbody>
</table>
Even while looping in table you can use the following command
<tbody>
<?
if(mysqli_num_rows($membersQuery) > 0){
while($member = mysqli_fetch_assoc($membersQuery)){
<tr>
<td><?php echo $member['firstname']; ?></td>
</tr>
<?php
}
}
?>
</tbody>
I'm currently working on a table that contains 'Name', 'Info', 'Price' and 'Duration'. However the PHP-Script is connected to a database.
I want to autofill the tablecells: 'Name', 'Price', 'Duration' via the Database by using PHP and SQL and that works perfectly fine for me. Though, I want to customize the content that's in the individual Info cells (e.g. Readmore jQuery and redirect to other pages).
I tried a little bit with using tags inside the Database and other weird stuff which, obviously, didn't work.
Is there a more elegant way of solving my problem than setting up a complete normal table without PHP/SQL, where I'd have to put in every bit of data about Name,Price and Duration manually?
<table class="table table-bordered table-hover tablesorter row sortable">
<thead>
<tr>
<th class="header">Name</th>
<th class="hidden-xs">Info</th>
<th>Price (in Euro)</th>
<th>Duration</th>
</tr>
</thead>
<tbody>
<?php
//Connect to Database
$db=mysql_connect ("xxxx", "xxxx", "xxxx") or die ('Oops! Da hat wohl' . mysqli_error(). 'Mist gebaut');
//Choose Database
$mydb=mysql_select_db("massageke_de");
//Query Database
$sql="SELECT * FROM Angebote";
//-run the query against the mysql query function
$result=mysql_query($sql);
//Show Results
while($row=mysql_fetch_array($result)){
//Start table ?>
<tr>
<td class="col-sm-2"><?php echo $Name =$row['Name'] ; ?></td>
<!--In this <td>-tag I want to put long textes with links-->
<td class="hidden-xs col-sm-5">echo $Name =$row['Info'];?<</td>
<td class="col-sm-2"><?php echo $Preis =$row['Preis']; ?></td>
<td ckass="col-sm-1"><?php echo $Dauer =$row['Dauer']; ?></td>
</tr>
<?php } ?>
</tbody>
</table>
Thanks in advance for helping.
Don't bother asking me additional questions.
P.S.: I hope you can help, without needing the CSS of Bootstrap, that I used.
P.P.S.:I know that my PHP-Script is not protected against PHP-Injection
(but I want to and will learn how to secure it)
Edit: As Jester asked for it. I made it quickly with photoshop because I think an image can express much better, what I want to achieve, than my poorly coding-skills.
Get to the image
Seems to me like the easiest way would be to just edit the info column in the database for each? If you want to do it in php i'd suggest making an array using the names (ids would be better but it seems you don't have access to those?) as keys:
$info['Aroma Teilkoerper'] = "Text about aroma teilkoerper";
$info['Aroma Ganzkoerper'] = "Text about aroma ganzkoerper";
and so on until you have all. Then in the loop:
//Show Results
while($row=mysql_fetch_array($result)){
//Start table ?>
<tr>
<td class="col-sm-2"><?php echo $Name =$row['Name'] ; ?></td>
<!--In this <td>-tag I want to put long textes with links-->
<td class="hidden-xs col-sm-5">echo $Name =$info[$row['Name']];?></td>
<td class="col-sm-2"><?php echo $Preis =$row['Preis']; ?></td>
<td ckass="col-sm-1"><?php echo $Dauer =$row['Dauer']; ?></td>
</tr>
<?php } ?>
Hoping that is a workable solution for you? (also you had a syntax error in your closing php tag.
echo $Name =$row['Info'];?< // <----- should be ?> of course
I had a need for a system that could track billable tasks for tickets that we processed. I have a script that is almost complete, but I cannot seem to figure out why the tickets_tasks_activity field is not pulling the right enum_short_description when displaying. When I add a task the values are added correctly, its only not displaying them correctly. It seems that I can add one activity... and then every additional activity has the same description even though the values are different in the database. Could someone please help me out.
Here is the script to pull the information from the database.
* Get the tasks associated with a ticket.
*
* #company_id The ID of the company that is being queried.
* #ticket_id The ID of the ticket that is being queried.
*/
function get_ticket_tasks($company_id, $ticket_id)
{
$sql = "SELECT u.user_first_name,
u.user_last_name,
u.user_id,
tn.tickets_tasks_posted_date,
tn.tickets_tasks_task,
tn.tickets_tasks_activity,
(SELECT enum_short_description FROM tbl_enum WHERE enum_id = tn.tickets_tasks_activity) tickets_tasks_activity_description
FROM tbl_tickets_tasks tn,
tbl_user u
WHERE tn.tickets_tasks_company_id = {$company_id}
AND tn.tickets_tasks_ticket_id = {$ticket_id}
AND tn.tickets_tasks_posted_user_id = u.user_id;";
$query = $this->db->query($sql);
// If nothing was returned invalid query.
if($query->num_rows() == 0)
return false;
return $query->result_array();
}
Here is the code for the View Page
<div class="tab-pane" id="tasks">
<div class="row">
<div class="col-lg-12">
<?php
if($tasks)
{ ?>
<table class="table table-striped table-bordered table-hover">
<thead>
<tr>
<th>Service Provided</th>
<th>Value</th>
<th>Who?</th>
<th>When?</th>
</tr>
</thead>
<tbody>
<?php
for($i = 0; $i < count($tasks); ++$i)
{ ?>
<tr>
<td><?php echo $tasks[0]['tickets_tasks_activity_description']; ?></td>
<td><?php echo nl2br($tasks[$i]['tickets_tasks_task']); ?></td>
<td><?php echo $tasks[$i]['user_first_name'].' '.$tasks[$i]['user_last_name']; ?></td>
<td><?php echo date("m/d/Y", strtotime($tasks[$i]['tickets_tasks_posted_date'])); ?></td>
</tr>
<?php
} ?>
</tbody>
</table>
<?php
}
else
{ ?>
<p>This ticket does not have any tasks.</p>
<?php
} ?>
</div>
</div>
</div>
Here you have (at the beginning of the loop):
<td><?php echo $tasks[0]['tickets_tasks_activity_description']; ?></td>
you're printing always $task[0], you should print $task[$i]
PS: You should probably state your JOIN in the FROM section of your SQL clause (instead to put it on the WERE section), at first I though you had none :-)