GET variable after doing a Join Query - php

For the code below, on the second link (http://www...com/sandbox/comments/index.php?submission='.$row["title"].'), I would like to pass $row["submissionid"], on as a GET variable. I tried this and it caused all of the code below to produce a blank result. Is there a way that I can do I want?
Thanks in advance,
John
$sqlStr = "SELECT
s.loginid
,s.title
,s.url
,s.displayurl
,l.username
,COUNT(c.commentid) countComments
FROM
submission s
INNER
JOIN
login l
ON
s.loginid = l.loginid
LEFT OUTER
JOIN
comment c
ON
s.submissionid = c.submissionid
GROUP
BY
s.submissionid
ORDER
BY
s.datesubmitted DESC
LIMIT
10";
$result = mysql_query($sqlStr);
$arr = array();
echo "<table class=\"samplesrec\">";
while ($row = mysql_fetch_array($result)) {
echo '<tr>';
echo '<td class="sitename1">'.$row["title"].'</td>';
echo '</tr>';
echo '<tr>';
echo '<td class="sitename2">'.$row["username"].''.$row["countComments"].'</td>';
echo '</tr>';
}
echo "</table>";

Add submissionid to your select.

Related

Joining three tables and displaying value using php

I have used join and was able to get output table in phpmyadmin using this query :
$query = "SELECT members.usn, members.name, events.ename FROM members JOIN participant ON members.usn = participant.usn JOIN events ON events.eid = participant.eid WHERE members.usn='.$usn.'";
$result = $conn->query($query);
The Output was
usn
name
ename
7DC18CS005
John
Robo Wars
I used PHP to display this in my homepage for specific user by maintaining usn in their SESSION.
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo '<tr>';
echo '<td>';
echo $row[0];
echo '</td>';
echo '<td>';
echo $row[1];
echo '</td>';
echo '<td>';
echo $row[2];
echo '</td>';
echo '</tr>';
}
} else {
echo "No Members[Connected DB]";
}
But the output gives me
"No Members[Connected DB]"
instead of expected output which is
usn
name
ename
7DC18CS005
John
Robo Wars
Thank you!
You have a typo in the query. Looks like you are trying to concatenate $usn to the query but you got it wrong.
Change
$query = "SELECT members.usn, members.name, events.ename FROM members JOIN participant ON members.usn = participant.usn JOIN events ON events.eid = participant.eid WHERE members.usn='.$usn.'";
To
$query = "SELECT members.usn, members.name, events.ename FROM members JOIN participant ON members.usn = participant.usn JOIN events ON events.eid = participant.eid WHERE members.usn='$usn'";

List Results in a Table - While Loop and List Results in TD Also

I have tried to find the answer for this but having no joy. I have a while loop that gives me numerous rows outputted in a table. It's here:
$result = mysqli_query($con,"SELECT a.winner AS horseWinner, a.twitter_pubstatus,
a.market, a.racetime, a.racecourse, a.date_dd, a.date_mm, b.username, b.course,
b.horse,b.type, b.racetime, b.dd, b.mm FROM results a
INNER JOIN bets b ON a.winner = b.horse WHERE a.twitter_pubstatus = 0
AND a.market = '$win' AND b.type = '$userwin'
AND a.date_dd = b.dd AND a.date_mm = b.mm GROUP BY a.winner");
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['horseWinner'] . "</td>";
echo "<td>" . $row['username'] . "</td>";
echo "</tr>";
}
echo "</table>";
This is all well and good but the issue lies that there is more than one username that is per horseWinner. I have tried to do another while loop within a while loop. So I would like it to list the usernames in the second column seperated by comma.
Many thanks
Add a GROUP_CONCAT() on your username so that it returns a comma separated list -
SELECT a.winner AS horseWinner, a.twitter_pubstatus,
a.market, a.racetime, a.racecourse, a.date_dd, a.date_mm,
GROUP_CONCAT(b.username) as username,
b.course, b.horse,b.type, b.racetime, b.dd, b.mm FROM results a
INNER JOIN bets b ON a.winner = b.horse WHERE a.twitter_pubstatus = 0
AND a.market = '$win' AND b.type = '$userwin'
AND a.date_dd = b.dd AND a.date_mm = b.mm GROUP BY a.winner

how to repeat entire table in while loop

I have a code which repeats rows while loop, but how can i repeat the entire table in while loop.
The code is
$kandivli = mysql_query("SELECT ps_order_detail.product_name, sum(ps_order_detail.product_quantity) AS product_total FROM ps_order_detail JOIN ps_product ON ps_product.id_product = ps_order_detail.product_id JOIN ps_orders ON ps_orders.id_order = ps_order_detail.id_order WHERE ps_product.id_supplier = '" . $supplier_number . "' && ps_orders.current_state = '" . $status1 ."' && ps_order_detail.id_shop = 1 GROUP BY ps_order_detail.product_name");
$ktotal = mysql_query("SELECT sum(ps_order_detail.product_quantity) AS product_total FROM ps_order_detail JOIN ps_product ON ps_product.id_product = ps_order_detail.product_id JOIN ps_orders ON ps_orders.id_order = ps_order_detail.id_order WHERE ps_product.id_supplier = '" . $supplier_number . "' && ps_orders.current_state = '" . $status1 ."' && ps_order_detail.id_shop = 1");
$krow = mysql_fetch_array($ktotal);
$kftotal = $krow['product_total'];
if(mysql_num_rows($kandivli) > 0)
{
echo '<table border="1" align="center" class="total">';
echo "<th> Product Name </td><th> Total Quantity </th>";
while($kandivlirow = mysql_fetch_array($kandivli))
{
$kandivli_product = $kandivlirow['product_name'];
$kandivlitotal = $kandivlirow['product_total'];
echo '<tr>';
echo "<td align='center'>$kandivli_product</td><tdalign='center'>$kandivlitotal</td>";
echo '</tr>';
}
echo "<td align='center'><b>Total</b></td><td align='center'><b>$kftotal</b></td>";
echo '</table>';
}
else echo "<div align='center'>No Pending orders</div>";
?>
Right now i just have 5 shops but if i add a new shop every time i have to copy paste and change this code to && ps_order_detail.id_shop = 6 then && ps_order_detail.id_shop = 7 and so on
So please if anyone shows how to repeat table. It would be grateful.
Hope my question is clear. (I want to repeat the entire table)
Regards
Amod
Ok I have Edited the code Now i get 6 tables as expected but the rows show the same data of shop one.
<?php
session_start();
include_once "connect.php";
?>
<?php
$supplier = mysql_query("SELECT ps_order_detail.product_name, sum(ps_order_detail.product_quantity) AS product_total, ps_supplier.name FROM ps_order_detail JOIN ps_product ON ps_product.id_product = ps_order_detail.product_id JOIN ps_supplier ON ps_supplier.id_supplier = ps_product.id_supplier JOIN ps_orders ON ps_orders.id_order = ps_order_detail.id_order WHERE ps_orders.current_state = 4 GROUP BY ps_order_detail.id_shop");
$alltotal = mysql_query("SELECT sum(ps_order_detail.product_quantity) AS producttotal FROM ps_order_detail JOIN ps_product ON ps_product.id_product = ps_order_detail.product_id JOIN ps_orders ON ps_orders.id_order = ps_order_detail.id_order WHERE ps_orders.current_state = 4 && ps_order_detail.id_shop = 11");
$ct = mysql_fetch_array($alltotal);
$ct1 = $ct['producttotal'];
while($supplierrow = mysql_fetch_array($supplier))
{
$kandivli = mysql_query("SELECT ps_order_detail.product_name, sum(ps_order_detail.product_quantity) AS product_total,ps_supplier.name FROM ps_order_detail JOIN ps_product ON ps_product.id_product = ps_order_detail.product_id JOIN ps_supplier ON ps_supplier.id_supplier = ps_product.id_supplier JOIN ps_orders ON ps_orders.id_order = ps_order_detail.id_order WHERE ps_orders.current_state = 4 GROUP BY ps_order_detail.product_name");
echo '<table border="1" align="center" class="total">';
while($kandivlirow = mysql_fetch_array($kandivli))
{
$sup = $kandivlirow['name'];
$sp = $kandivlirow['product_name'];
$spt = $kadivlirow['product_total'];
echo '<tr>';
echo "<td align='center'>$sp</td><td align='center'>$spt</td><td align='center'>$sup</td>";
echo '</tr>';
}
echo '</table>';
echo '<br>';
}
echo "<td align='center'><b>Total</b></td><td align='center'><b>$ct1</b></td>";
?>
while($kandivlirow = mysql_fetch_array($kandivli))
{
//Your table code
echo '<table border="1" align="center" class="total">';
//Middle section
echo '</table>';
}

Table not Echoing out if another Table has a Zero value

The HTML table below with mysql_query($sqlStr3) (the one with the word "Joined" in its row) does not echo if the result associated with mysql_query($sqlStr1) has a value of zero. This happens even if mysql_query($sqlStr3) returns a result.
In other words, if a given loginid has an entry in the MySQL table "login", but not one in the MySQL table "submission", then the table associated with mysql_query($sqlStr3) does not echo.
I don't understand why the MySQL "submission" table would have any effect on mysql_query($sqlStr3), since the $sqlStr3 only deals with another MySQL table, called "login", as seen below.
Any ideas why this is happening?
Thanks in advance,
John W.
<?php
echo '<div class="profilename">User Profile for </div>';
echo '<div class="profilename2">'.$profile.'</div>';
$tzFrom = new DateTimeZone('America/New_York');
$tzTo = new DateTimeZone('America/Phoenix');
$profile = mysql_real_escape_string($_GET['profile']);
$sqlStr = "SELECT l.username, l.loginid, s.loginid, s.submissionid, s.title, s.url, s.datesubmitted, s.displayurl
FROM submission AS s
INNER JOIN login AS l
ON s.loginid = l.loginid
WHERE l.username = '$profile'
ORDER BY s.datesubmitted DESC";
$result = mysql_query($sqlStr);
$arr = array();
echo "<table class=\"samplesrec1\">";
while ($row = mysql_fetch_array($result)) {
$dt = new DateTime($row["datesubmitted"], $tzFrom);
$dt->setTimezone($tzTo);
echo '<tr>';
echo '<td class="sitename3">'.$dt->format('F j, Y &\nb\sp &\nb\sp g:i a').'</a></td>';
echo '<td class="sitename1">'.$row["title"].'</td>';
echo '</tr>';
}
echo "</table>";
$sqlStr1 = "SELECT l.username, l.loginid, s.loginid, s.submissionid, s.title, s.url, s.datesubmitted, s.displayurl, l.created, count(s.submissionid) countSubmissions
FROM submission AS s
INNER JOIN login AS l
ON s.loginid = l.loginid
WHERE l.username = '$profile'";
$result1 = mysql_query($sqlStr1);
$arr1 = array();
echo "<table class=\"samplesrec2\">";
while ($row1 = mysql_fetch_array($result1)) {
echo '<tr>';
echo '<td class="sitename5">Submissions: '.$row1["countSubmissions"].'</td>';
echo '</tr>';
}
echo "</table>";
$sqlStr2 = "SELECT l.username, l.loginid, c.loginid, c.commentid, c.submissionid, c.comment, c.datecommented, l.created, count(c.commentid) countComments
FROM comment AS c
INNER JOIN login AS l
ON c.loginid = l.loginid
WHERE l.username = '$profile'";
$result2 = mysql_query($sqlStr2);
$arr2 = array();
echo "<table class=\"samplesrec3\">";
while ($row2 = mysql_fetch_array($result2)) {
echo '<tr>';
echo '<td class="sitename5">Comments: '.$row2["countComments"].'</td>';
echo '</tr>';
}
echo "</table>";
$tzFrom3 = new DateTimeZone('America/New_York');
$tzTo3 = new DateTimeZone('America/Phoenix');
$sqlStr3 = "SELECT created, username
FROM login
WHERE username = '$profile'";
$result3 = mysql_query($sqlStr3);
$arr3 = array();
echo "<table class=\"samplesrec4\">";
while ($row3 = mysql_fetch_array($result3)) {
$dt3 = new DateTime($row3["created"], $tzFrom3);
$dt3->setTimezone($tzTo3);
echo '<tr>';
echo '<td class="sitename5">Joined: '.$dt->format('F j, Y').'</td>';
echo '</tr>';
}
echo "</table>";
?>
</body>
</html>
If I understand the problem correctly, you need to echo out $dt3->format('F j, Y') instead of $dt->format('F j, Y') in one of the last lines of the code you have.
while ($row3 = mysql_fetch_array($result3)) {
$dt3 = new DateTime($row3["created"], $tzFrom3);
$dt3->setTimezone($tzTo3);
echo '<tr>';
echo '<td class="sitename5">Joined: '.$dt->format('F j, Y').'</td>';
echo '</tr>';
}
echo "</table>";
Are you looking to call $dt3->format('F j, Y') in the echo here, instead of $->format('F j, Y')? I don't see any other use for $dt3.

Query not returning results when a "WHERE" condition is added to it

The query below is not returning any results. I have copied it from a query that was working and the only thing different is the addition of the following condition:
WHERE submissionid=$submissionid
Any ideas why this query is not returning any results?
$sqlStr2 = "SELECT
s.loginid
,s.title
,s.url
,s.displayurl
,s.datesubmitted
,l.username
,s.submissionid
,COUNT(c.commentid) countComments
FROM submission s
INNER JOIN login l ON s.loginid = l.loginid
LEFT OUTER JOIN comment c ON s.submissionid = c.submissionid
GROUP BY s.submissionid
WHERE submissionid=$submissionid
ORDER BY s.datesubmitted DESC
LIMIT 10";
$tzFrom2 = new DateTimeZone('America/New_York');
$tzTo2 = new DateTimeZone('America/Phoenix');
$result2 = mysql_query($sqlStr2);
$arr2 = array();
echo "<table class=\"samplesrec\">";
while ($row2 = mysql_fetch_array($result2)) {
$dt2 = new DateTime($row2["datesubmitted"], $tzFrom2);
$dt2->setTimezone($tzTo2);
echo '<tr>';
echo '<td class="sitename1">'.$row2["title"].' <div class="dispurl">'.$row2["displayurl"].'</div></td>';
echo '</tr>';
echo '<tr>';
echo '<td class="sitename2name">Submitted by '.$row2["username"].' on '.$dt2->format('F j, Y &\nb\sp &\nb\sp g:i a').'</td>';
echo '</tr>';
echo '<tr>';
echo '<td class="sitename2">'.$row2["countComments"].' comments</td>';
echo '</tr>';
}
echo "</table>";
Just a hunch, but are you sure there are any rows that meet the condition?
WHERE submissionid=$submissionid
Also, have you checked the value of $submissionid to make sure you know what is in it when the query returns no rows?
There is c.submissionid and s.submissionid -- maybe You should state in Your query which one You want to use. Example: WHERE s.submissionid=$submissionid
I think GROUP BY should go after WHERE clause.
Try this:
$result2 = mysql_query($sqlStrt2) or die("Error " . mysql_error());
To see if there is a problem with the query.
I don't really use php but it looks like it might not be filling in the variable. So maybe do:
WHERE submissionid=".$submissionid."
Just to be sure print out $sqlStr2 to see what it is.
your query $sqlStr2 is not quieted.it's be usefully quiet all fisrt and i guess this WHERE submissionid=$submissionid should like this WHERE submissionid=\'$submissionid\'.

Categories