Currently, my plan is to add a gender icon next to the username. The problem is that even if there are more than 1 table record in the table, it shows only 1 row and some records suddenly gone. This is how it looks like:
The code I am using:
$pdo = new PDO('connection');
$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt1 = $pdo->prepare("SELECT id, user_id, user, bid, date
FROM auction_bids ORDER BY date DESC");
$stmt2 = $pdo->prepare("SELECT user_id, user, bid, date
FROM auction_bids ORDER BY date DESC LIMIT 30");
$stmt11 = $pdo->prepare("SELECT pol FROM tb_users WHERE id = :user_id");
$stmt2->execute();
$r1 = $stmt1->fetch(PDO::FETCH_ASSOC);
$stmt11->execute(array(':user_id' => $r1['user_id']));
$id1 = $r1['id'];
echo '<table>';
while ($r1 = $stmt2->fetch(PDO::FETCH_ASSOC) && $r2 = $stmt11- >fetch(PDO::FETCH_ASSOC)) {
echo '<tr>
<td>' . $id1 . '</td>
<td><img height="16" width="16" alt="gender icon" src
="../images/' . ($r2['pol'] == 1 ? 'male.png' : 'female.png') . '" />
' . $r1['user'] . '</td>
<td class="border-right">' . $r10['level'] . '</td>
<td>' . $r1['bid'] . ' FA</td>
<td><img height="16" width="16" alt="calendar" src
="../images/calendar.png" />
' . date($dateFormatBids, strtotime($r1['date'])) . '</td>
</tr>
';
$id1--;
}
echo '<table>
Using while ($r1 = $stmt2->fetch(PDO::FETCH_ASSOC)) {..} loop without $r2, it is working (except that the gender color is the same, for test it should be red, for Lukas, should be blue):
If there is something that you need, please say. I am quite new to PHP and PDO.
from the line: $stmt11->execute(array(':user_id' => $r1['user_id'])); you are getting a single record so the while loop prints single row.
Write the query like:
$stmt2 = $pdo->prepare("SELECT tu.pol, user_id, user, bid, date
FROM auction_bids ab left join tb_users tu on ab.user_id = tu.id
ORDER BY date DESC LIMIT 30");
now :
while ($r1 = $stmt2->fetch(PDO::FETCH_ASSOC){
//rest of the code should work... user $r1['pol'] instead of $r2
}
Check that link to understand while loop :
http://www.w3schools.com/js/js_loop_while.asp
The problem you have condition should TRUE, and you ($r1 && $r2) = TRUE only when both Variable are TRUE, $r2 first time it's TRUE both second time will get FALSE.
You should loop for only $r1 and call $r1 inside the loop.
Related
I have two SQL queries. The first pulls a list of users and then the second pulls a sales report. After the report is pulled, I want to generate a PDF for each employee based on their sales. The problem is that I can't get the nested while loop to print more than only 1 record for the employee before going onto the next employee. I tried referencing userid's, but it just prints all of the lines to the first userid in the array then. If someone could tell me what I am missing, I would GREATLY appreciate it... I have been staring at this for like five hours today.
$stmt2 = $con->prepare("select user_id from users");
$stmt2->execute();
$stmt = $con->prepare("select u.user_id, u.fname, u.lname, d.saledate, d.custname, d.straddr, d.city, d.state, d.zip
from users u
inner join dplgalionsales d
on u.lname = d.agent_last_name
where saledate > DATE_SUB(NOW(), INTERVAL 1 MONTH)");
$stmt->execute();
while($usrid = $stmt2->fetch()) {
$uid = $usrid['user_id'];
$mydate = date('m/d/Y');
$dateadd = date('m/d/Y', strtotime($mydate. ' + 3 days'));
$html_table = '<div>Week Ending: ' .$mydate. '<br>Payroll Issued: ' .$dateadd. '</div><br>';
$html_table .= '<table border="1" cellspacing="0" cellpadding="2" width="100%"><tr><th>Date</th><th>Customer Name</th><th>Address</th></tr>';
while($result = $stmt->fetch()) {
$resuser = $result['user_id'];
if($resuser = $uid) {
$html_table .= '<tr><td>' .$result['saledate']. '</td><td>' .$result['custname']. '</td><td>' .$result['straddr']. ' ' .$result['city']. ' ' .$result['state']. ' ' .$result['zip']. '</td></tr>';
} else {
break;
}
}
$html_table .= '</table>'; //ends HTML table
$mpdf = new mPDF();
$mpdf->SetTitle('DPL Galion Sales');
$mpdf->WriteHTML($html_table);
$mpdf->Output('./reports/'.$uid.'/'.date('m-d-Y').'_SalesID_'.$uid.'.pdf','F');
}
exit;
The following line is incorrect:
if($resuser = $uid)
For comparison it should be == or ===
Also try to combine this query into a single one if possible
I have a custom web store and it was made with php. It gets all my products from certain categories that are stored in a mySQL database, and displays them in tables for my customers. The programming looks like it is meant to sort them by date added, but that doesn't work. I have messed with it several times trying different variables with no help. Is there something wrong with the syntax here?
This is the code for the actual list (above the on the page):
$dynamicList = "";
$sql = mysql_query("SELECT * FROM products ORDER BY date_added DESC LIMIT 5");
$productCount = mysql_num_rows($sql); // count the output amount
if(isset($_GET['category'])) {
$category = $_GET['category'];
$sql = mysql_query("SELECT * FROM products WHERE category = '$category'");
$num_rows = mysql_num_rows($sql);
if($num_rows > 0) {
while($row = mysql_fetch_array($sql)) {
$id = $row["id"];
$product_name = $row["product_name"];
$price = $row["price"];
$date_added = strftime("%b %d, %Y", strtotime($row["date_added"]));
$dynamicList .= '<table width="65%" border="15" cellspacing="0" cellpadding="6" align="center">
<tr>
<td width="17%" valign="top"><img style="border:#000000 1px solid;" src="inventory_images/' . $id . '.jpg" alt="' . $product_name . '" width="155" height="155" border="1" /></td>
<td width="83%" valign="center">' . $product_name . '<br />
$' . $price . '<br />
<img src="style/detailssm.png" border="0" alt="Tube Bender"></td>
</tr>
</table>';
}
}
}
mysql_close();
?>
My product database has all the attributes listed in the code above, and they are all filled out (like product_name, date_added, category, etc). I would really like to be able to get the sort by date to work, or add a new column like "display order" and then I just number them all and be done with it. But I can't just replace the variable since the current one doesn't work anyway : (
Let me know if you guys have any ideas. Thank you!
You have 2 sql statements in your sample code. The first has an ORDER BY clause, but doesn't appear to be used anywhere.
The second SQL statement has no ORDER BY clause, and that's the one you use to build your dynamicList table.
I think that the second query just to have the "order by", try this...
<?php
$dynamicList = "";
$sql = mysql_query("SELECT * FROM products ORDER BY date_added DESC LIMIT 5");
$productCount = mysql_num_rows($sql); // count the output amount
if(isset($_GET['category'])) {
$category = $_GET['category'];
$sql = mysql_query("SELECT * FROM products WHERE category = '$category' ORDER BY date_added DESC ");
$num_rows = mysql_num_rows($sql);
...
?>
My database table have 5 columns: 'id', 'date_visited', 'page_title', 'ip' and 'total_views'.
I am not able to display ORDER BY 'date_visited'.
My PHP Query is:
<?php
[...]
$query = "SELECT *,count(*) FROM table WHERE ip GROUP BY page_title";
$result = mysqli_query($link,$query) or die(mysqli_error($link). "Q=".$query);
if(!$result == 0) {
while ($row = mysqli_fetch_array($result)) {
$dataList_br .= '<tr>
<td>' .$row['date_visited']. '</td>
<td>' .$row['page_title']. '</td>
<td>' .$row['count(*)']. '</td>
</tr>';
}
} else {
$dataList_br .= '<p class="warning">No data found in database.</p>';
}
?>
When it outputs, it displays [date][page title] and [total views].
Please someone help me, how do I display last date from the query, instead now it displays the very first day the page was visited.
Thank you.
MySQL is lenient about the contents of the GROUP BY and will return a row for the group somewhat arbitrarily if columns aren't in the GROUP BY but are SELECTed. In your case, it just gave you the first row (lowest date) for each group.
Get the page_name of the row with the MAX(date_visited) per group and join that against the main table to pull in the remaining columns from the main table.
SELECT
table.id,
table.ip,
table.total_views,
maxdates.date_visited,
maxdates.page_name,
maxdates.thecount
FROM
table
JOIN (
/* Subquery returns the aggregates to join against
the main table so other columns can be pulled in */
SELECT
page_title,
MAX(date_visited) AS maxdate,
COUNT(*) AS thecount
FROM table
GROUP BY page_title
) maxdates
ON table.page_name = maxdates.page_name
AND table.date_visited = maxdates.maxdate
You want to use ORDER BY. Like so:
$query = "SELECT *, count(*)
FROM table
WHERE ip
GROUP BY page_title
ORDER BY date_visited ASC";
Try this whit "ORDER BY":
$query = "SELECT date_visited, page_title,count(*) FROM table WHERE ip ORDER BY page_title ASC";
$result = mysqli_query($link,$query) or die(mysqli_error($link). "Q=".$query);
if(!$result == 0) {
while ($row = mysqli_fetch_array($result)) {
$dataList_br .= '<tr>
<td>' .$row['date_visited']. '</td>
<td>' .$row['page_title']. '</td>
<td>' .$row['count(*)']. '</td>
</tr>';
}
} else {
$dataList_br .= '<p class="warning">No data found in database.</p>';
}
Im trying to build a feeds application in php in which im using a simple table with 2 columns..one for name and one for the respective feeds...my feed is appearing properly but there is some problem with the name heres the code...
<?php
include_once "connect_to_mysql.php";
$sql = mysql_query("SELECT id, feed, feeddate FROM feeds ORDER BY feeddate DESC LIMIT 20");
while($row = mysql_fetch_array($sql))
{
$name = $row["name"];
$uid = $row["userid"];
$ufeed = $row["feed"];
$feeddate = $row["feeddate"];
$feeds .= '
<table width="90%" align="center" cellpadding="4" bgcolor="#A6D2FF">
<tr>
<td width="7%" bgcolor="#FFFFFF">' . $name . '<br />
</td>
<td width="93%" bgcolor="#D9ECFF"> <span style="font-size:10px; font-weight:bold; color:#A6A6A6;">' . $feeddate . '</span><br />
' . $ufeed . '</td>
</tr>
</table>';
}?>
<?php print "$feeds"; ?>
here the $name thing is simply not displaying as a link!please help..
You are selecting only three columns:
SELECT id, feed, feeddate
"name" is not among them, so it will be always empty.
$row["name"] is not set, because you did not include the name column in the select expression of the database query. Consequently, the statement
$name = $row["name"];
sets $name to null. The same holds for $uid and $row["userid"].
Your sql query should be
$sql = mysql_query("SELECT name, id, feed, feeddate FROM feeds ORDER BY feeddate DESC LIMIT 20");
Also, you have $uid = $row["userid"];. I believe it should be $uid = $row["id"]; as you have id in sql query. You have to change either of them.
If you have id use above query, else use below query.
$sql = mysql_query("SELECT name, userid, feed, feeddate FROM feeds ORDER BY feeddate DESC LIMIT 20");
You are selecting 3 columns id,feed,feed date.But you are printing name,id,feed,feed date. Please select name column also. It will display proper output.
Hi i have the following query/table from a local bookstore
$queryadmin ="SELECT last_name, first_name, user_id FROM users";
$recordz = #mysqli_query ($dbc, $queryadmin);
while ($row = mysqli_fetch_array($recordz, MYSQLI_ASSOC)) {
echo '<tr>
<td align="left">' . $row['first_name'] . '</td>
<td align="left">' . $row['last_name'] . '</td>
<td align="left">' . $row['user_id'] . '</td>
</tr>'
;}
now i want an additional column from another table where the numbers of books each user has lend out are displayed.
So the query if nested seperately would go something like this
$query2 = mysql_query("SELECT FROM mirror3 WHERE userid='".$row['user_id']."'", $link);
$anzahl = mysql_num_rows($query2);
placing this query nested inside the while query (right after while starting) from above does not work. How to do that?
supplied argument is not a valid MySQL
result resource
thanks
Your query is wrong. Specify a field name:
$query2 = mysql_query("SELECT
SOMETHING FROM mirror3 WHERE userid='".$row['user_id']."'", $link);
You could probably do this in one query:
$query = mysqli_query('SELECT u.last_name, u.first_name, u.user_id, m.PUT_SOMETHING_HERE
FROM users u
LEFT JOIN mirror3 m ON u.user_id = m.userid
WHERE m.PUT_SOMETHING_HERE IS NOT NULL');
But Parkyprg has a point, you need to be selecting something from that second query.