I'm creating a page where it gets contact details from the forum on registered members. I've hit a little problem trying to retrieve the custom fields. For University and flights a drop down menu is used so that users can select from a specific list. However the forum uses 3 different table to store the data.
This is a small part of the tables
db mock up http://www.emuas.co.uk/images/db_mock_up.png
at the moment I get the uni for the uni query and a flight for the flight query, but the result is unrelated to the user_id. Not sure how to change that though.
$user = "SELECT *
FROM profile_fields_data
JOIN users
ON profile_fields_data.user_id = users.user_id
ORDER BY users.username";
$uni = "SELECT *
FROM profile_fields_data
JOIN users
ON profile_fields_data.user_id = users.user_id
JOIN profile_fields_lang
ON profile_fields_data.pf_university = profile_fields_lang.option_id
WHERE field_id =4
ORDER BY users.username";
$flight = "SELECT *
FROM profile_fields_data
JOIN users
ON profile_fields_data.user_id = users.user_id
JOIN profile_fields_lang
ON profile_fields_data.pf_flight = profile_fields_lang.option_id
WHERE field_id =3
ORDER BY users.username";
$userdata = $db->sql_query($user);
$unidata = $db->sql_query($uni);
$flightdata = $db->sql_query($flight);
while($data = $db->sql_fetchrow($userdata))
{
$datauni = $db->sql_fetchrow($unidata);
$dataflight = $db->sql_fetchrow($flightdata);
echo "<tr>
<td> <a href='http://emuas.co.uk/forum/memberlist.php?mode=viewprofile&u=" . $data['user_id'] . "'>" . $data['username'] . "</a></td>
<td> <a href='mailto:" . $data['user_email'] . "'>" . $data['user_email'] . "</a> </td>
<td>" . $data['pf_contact_number'] . "</td>
<td>" . $data['user_birthday'] . "</td>
<td>" . $data['pf_service_number'] . "</td>
<td>" . $datauni['lang_value'] . "</td>
<td>" . $dataflight['lang_value'] . "</td>
<td>" . $data['pf_secondary_duty'] . "</td>
</tr>";
Many thanks
Found the problem. When the forum got the data for option_id it added 1 to the value. I;ve changed it to take this into account
Related
How do i replace column name 'product_name' to 'TOTAL' and other column name 'inv_date' to 'TOTAL'. Tried with as below but to no avail.
$query = "SELECT IFNULL(pm.product_name,'TOTAL') product_name,IFNULL(pt.inv_date,'TOTAL') inv_date, SUM(pt.qty) as total_qty,(SUM(pt.amount)/SUM(pt.qty)) as avg_rate, SUM(pt.amount) as total_amount1,SUM(pt.tcs) as total_tcs, SUM(pt.total_amount) as total_amount_net,
am.acc_name,pgm.product_group,psgm.sub_product_group,pim.pre_insp_name, ptm.purch_type_name, pt.* FROM purch_tble pt LEFT JOIN product_master pm ON pm.product_id = pt.product_id LEFT JOIN acc_master am ON am.acc_id = pt.acc_id LEFT JOIN purch_type_master
ptm ON ptm.purch_type_id = pt.purch_type_id LEFT JOIN prod_group_master pgm ON pgm.prod_group_id = pm.prod_group_id LEFT JOIN prod_sub_group_master psgm ON psgm.prod_sub_group_id = pm.prod_sub_group_id LEFT JOIN pre_insp_master pim ON pim.pre_insp_id
= pt.pre_insp_id $where GROUP BY pt.product_id, DATE_FORMAT(pt.inv_date,'%Y-%m') WITH ROLLUP"; $retval = mysql_query($query); $num_row = mysql_num_rows($retval); echo mysql_error(); if($num_row >= 1){ while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
{ echo "
<tr>"; echo "
<td>" . date('m-Y',strtotime($row['inv_date']))."</td>"; echo "
<td>" . $row['product_name']."</td>"; echo "
<td>" . round($row['total_qty'])."</td>"; echo "
<td>" . round($row['avg_rate'],2)."</td>"; echo "
<td>" . round($row['total_tcs'])."</td>"; echo "
<td>" . round($row['total_amount1'])."</td>"; echo "
<td>" . round($row['total_amount_net'])."</td>"; echo "</tr>"; } // while loop finishes } // if statement finishes
I also tried with following but to no avail.
COALESCE(pm.product_name,'TOTAL') product_name,
COALESCE(pt.inv_date,'TOTAL') inv_date,
LASTLY, I also tried with following but to no avail.
IF(GROUPING(pm.product_name),'TOTAL',pm.product_name) product_name,
IF(GROUPING(pt.inv_date),'TOTAL',pt.inv_date) inv_date,
The picture is self explanatory.
I am stuck don't where am I going wrong.
Hope I have been able to express question clearly.
I'm trying to get information about an SQL table via a php script however, whenever I add a SUM function I get the following error "Notice: Trying to get property 'num_rows' of non-object"
I don't know what's going on but if anyone can please explain/point me in the right direction as to how to fix this problem that would be greatly appreciated
Here's the 'broken' code
<?php
require("connect.php");
$inNo = $_POST["inNo"];
$sql = "SELECT invoice.invoice_no, invoice.date, invoice.cust_id, invoice.emp_id, invoice_line.prod_id,
invoice_line.qty, product.cost_price, (product.cost_price * invoice_line.qty) AS `multi`,
customer.first_name AS `customer_fname`, customer.last_name AS `customer_lname`,
employee.first_name AS `emp_fname`, employee.last_name AS `emp_lname`,
product.name AS `proname`
FROM invoice INNER JOIN invoice_line ON invoice.invoice_no = invoice_line.invoice_no
INNER JOIN product ON invoice_line.prod_id = product.id
INNER JOIN customer ON invoice.cust_id = customer.id
INNER JOIN employee ON invoice.emp_id = employee.id, SUM(multi) AS `invoicetotal`
WHERE cust_id = '" . $inNo . "'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
//open table
echo '<table class="table table-striped" id="outTable">';
echo "<tr><th>Product</th><th>Qty</th><th>Price</th><th>Total cost</th></tr>";
// output data of each row
$ctr = 0;
while($row = $result->fetch_assoc()) {
if ($ctr == 0)
{
echo "<h1>Invoice Number: " . $row["invoice_no"]. "</h1>";
echo "<p>Customer: " . $row["customer_fname"]. " " . $row["customer_lname"]."</p>";
echo "<p>Employee: " . $row["emp_fname"]. " " . $row["emp_lname"]. "</p>";
echo "<p>Date: " . $row["date"]. "</p>";
}
echo "
<tr>
<td>" . $row["proname"]. "</td>
<td>" . $row["qty"]. "</td>
<td>" . $row["cost_price"]. "</td>
<td>" . $row["multi"]. "</td>
</tr>";
$ctr++;
}
} else {
echo "0 results";
}
$conn->close();
?>
You need to insert the SUM() in the SELECT part above FROM:
SELECT
invoice.invoice_no,
invoice.date,
invoice.cust_id,
invoice.emp_id,
invoice_line.prod_id,
invoice_line.qty,
product.cost_price,
(product.cost_price * invoice_line.qty) AS `multi`,
customer.first_name AS `customer_fname`,
customer.last_name AS `customer_lname`,
employee.first_name AS `emp_fname`,
employee.last_name AS `emp_lname`,
product.name AS `proname`,
SUM(multi) AS `invoicetotal`
FROM invoice
INNER JOIN invoice_line
ON invoice.invoice_no = invoice_line.invoice_no
INNER JOIN product
ON invoice_line.prod_id = product.id
INNER JOIN customer
ON invoice.cust_id = customer.id
INNER JOIN employee
ON invoice.emp_id = employee.id` WHERE cust_id = 99
BTW, it's wise to format code - SQL code too - fine.
I am working in php and bootstrap with tables.
I have a tableA that for each row contains a button.
Each button has a value assigned equal to the row number. (see below)
$job_id = $row['id'];
<td> <a class='btn btn-primary btn-sm' data-toggle='modal' data-target='#myModal' name='job_id' value=$job_id id='job_id'>Info</a></td>
I cannot achieve this:
- Each button open a modal pop-up that contains the row of a tableB
So if I click on the button of the second row of tableA I want to show in the popup all the data that row2 of the tableB contains and so on.
Basically I cannot find out how to connect the button with a specific row of another table.
With this code in my modal I can only gather the entire tableB, but not the single row.
$sql = "SELECT * from tableb WHERE id=job_id ";
With this code in my modal I gather always the row 3 of tableB for all the buttons...
$sql = "SELECT * from tableb WHERE id=3 ";
With this code in my modal I gather for every button the last row I inserted
$sql = "SELECT * from tableb WHERE id='$job_id' ";
(TableA and TableB are connected with foreign key)
TableB has (id, job_id) with job_id= id of tableA
TableA has (id)
Any suggestion that can help me with what I want to achieve?
UPDATE 1 -> File xxx.php containts the script that load TableA and the script that load the Modal pop up
script that load TableA
<?php
include("../includes/connection.php");
if ($link->connect_errno > 0) {
die('Unable to connect to database [' . $link->connect_error . ']');
}
$sql = "SELECT * from TableA";
if (!$result = $link->query($sql)) {
die('There was an error running the query [' . $link->error . ']');
}
echo "
<table class='table'>
<thead>
<tr>";
/* Get field information for all columns */
...
while ($row = $result->fetch_assoc()) {
$job_id = $row['id'];
echo "<form action='' method=post>";
echo "<tr class='info'>
<input type=hidden name=hidden value=" . $row['id'] . ">
<td>" . $row['id'] . "</td>
<td>" . $row['device'] . "</td>
<td>" . $row['model'] . "</td>
<td>" . $row['problem'] . "</td>
<td> <a class='btn btn-primary btn-sm' data-toggle='modal' data-target='#myModal' name='job_id' value=[$job_id] > Info</a></td>
</tr>";
echo "</form>";
} echo " </tbody> </table>"; ?>
script that load the modal popup with tableB
<?php
include("../includes/connection.php");
if ($link->connect_errno > 0) {
die('Unable to connect to database [' . $link->connect_error . ']');
}
$sql = "SELECT * from TableB WHERE job_id=$job_id ";
if (!$result = $link->query($sql)) {
die('There was an error running the query [' . $link->error . ']');
}
echo "
<table class='table'>
<thead>
<tr>";
/* Get field information for all columns */
while ($finfo = $result->fetch_field()) {
echo "
<th>" . $finfo->name . "</th>";
}
echo "
</tr>
</thead>
<tbody>";
while ($row = $result->fetch_assoc()) {
echo "<tr class='info'>
<td>" . $row['id'] . "</td>
<td>" . $row['name'] . "</td>
<td>" . $row['mail'] . "</td>
<td>" . $row['number'] . "</td>
<td>" . $row['price'] . "</td>
<td>" . $row['paymenttype'] . "</td>
<td>" . $row['faktura'] . "</td>
<td>" . $row['date'] . "</td>
</tr>";}echo " </tbody></table>";?>
You can use JOIN instruction to link your two tables with one single request.
// For the third button the SQL request will be :
$sql="SELECT * FROM tableA A INNER JOIN tableb B ON B.job_id=A.id WHERE B.id=3";
// For the fourth button the SQL request will be :
$sql="SELECT * FROM tableA A INNER JOIN tableb B ON B.job_id=A.id WHERE B.id=4";
Note that here I use aliases on my request. For instance A is the alias of table A. Thus, A.id is the id column of tableA.
Look at this link for more infos about SQL Joins : http://www.w3schools.com/sql/sql_join.asp.
To get the rows in tableB corresponding to an id of tableA you should write:
$sql = "SELECT * from tableb WHERE job_id = $jobId ";
Note that I put 'WHERE job_id = ', and not 'WHERE id = '.
There's an answer that recommends a JOIN. With a JOIN you'll get the row in tableA and the data in tableB
I have a table "data" with a column "name" "state" and a few more
name state
peter MN
john NY
jay NY
sam CO
jack TX
jill NO
I want to calculate the number of entries per state and want my output as follows for example:
NY: 125
MN: 21
CO: 17
TX: 10
NO: 59
etc...
I have a query like this
$stmt = $db->query("SELECT state, COUNT(*) FROM `data` GROUP BY state;");
$nums = $stmt->rowCount();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
echo "<tr>
<td>" . $row["state"] . "</td>
<td>$nums</td>
</tr>";
}
This displays every state in my table but does not return the corresponding number of entries for that state. This only returns the number of states i.e. 50. How can I display the number of entries per state?
You seem not to be referring to the column which has the count. Try aliasing it an referencing the alias in your PHP code:
$stmt = $db->query("SELECT state, COUNT(*) cnt FROM `data` GROUP BY state;");
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
echo "<tr>
<td>" . $row["state"] . ":</td>
<td>" . $row["cnt"] . "</td>
</tr>";
}
$stmt = $db->query("SELECT COUNT(name) as occurances,state FROM `data` GROUP BY state;");
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
echo "<tr>
<td>" . $row["occurances"] . "</td>
<td>" . $row["state"] . "</td>
</tr>";
}
Try this version,select the names only
Total newbie trying to learn... thanks for all the help so far!
UPDATE - updated code below - Thanks Phill
I'm now trying to get the relevant horse information from a table horses which I can join to race_form with the horse_name field. I want to then display the information for each horse in the race below the while($racecard) info. Does this make it a bit clearer?
I thought I could just do another query and then a while loop with mysql_fetch_array below the one I have already and that would display it and then move onto the next race, but that apparently doesn't work...?!
I'm wondering if you can run 2 while loops after each other inside a while loop? I am working on a horse racing formguide - I can display each race list, but underneath I want to display individual horse details on each horse in the race. Anyway if you look at this page you can see what I'm trying to do:
http://tasmanianracing.com/superform/formguide.php?meetingID=21042011LAUN&Submit=View+Form
Problem is, any time I put a second while loop after the race list, it won't show up. I'm trying to run a query to get the horse details from my horse table and then run a while for each horse in the race, but nothing shows up.
I hope this is clear enough ... my snippet of code is below - please let me know if I've missed out something important:
echo "<table width='990' border='1' cellspacing='2' cellpadding='2'>";
$racedetails = mysql_query("SELECT *
FROM race_info
WHERE meetingID = ('" . $safemeetingID . "')");
while($row = mysql_fetch_array($racedetails))
{
echo "<tr><td>Race " . $row['race_number'] . " at " . $row['start_time'] . " " . $row['class'] . " over " . $row['distance'] . "m</td></tr>";
$racecard = mysql_query("SELECT *
FROM race_form
INNER JOIN race_info ON race_form.raceID = race_info.raceID
WHERE race_form.raceID = ('" . $row['raceID'] . "')");
while($row = mysql_fetch_array($racecard))
{
echo "<tr><td>" . $row['number'] . "</td><td>" . $row['past10'] . "</td><td>" . $row['horse_name'] . "</td></tr>";
}
echo "</table><br>";
echo "<br>I wish I could put in some horse form here...<br>";
echo "<table width='990' border='1' cellspacing='2' cellpadding='2'>";
}
echo "</table>";
You'll probably need to change the names of some of the $row variables, they may interfere with each other.
For example:
while($row_races = mysql_fetch_array($totalraces)){
while($row_details = mysql_fetch_array($racedetails)){
while($row_card = mysql_fetch_array($racecard)){
I think you can get rid of one of your queries:
The first query gets the number of races by selecting a COUNT, This returns one record with the count value.
// This returns one record with the count
$total_races = "SELECT COUNT(race_number) AS totalraces
FROM race_info WHERE meetingID = ('".$safemeetingID."') ";
Next you iterate over the the same records as the rows returned for the race details are the same as the count.
// This looks to return the record(s) with the race details
$race_details = "SELECT * FROM race_info
WHERE meetingID = ('" . $safemeetingID . "')";
I think you can just use this to get the desired results: (I agree to rename the $row variable(s) to something descriptive for each while loop)
$racedetails = mysql_query("SELECT *
FROM race_info
WHERE meetingID = ('" . $safemeetingID . "')");
while($details_row = mysql_fetch_array($racedetails))
{
echo "<tr><td>Race " . $details_row['race_number'] . " at " . $details_row['start_time'] . " " . $details_row['class'] . " over " . $details_row['distance'] . "m</td></tr>";
$racecard = mysql_query("SELECT *
FROM race_form
INNER JOIN race_info ON race_form.raceID = race_info.raceID
WHERE race_form.raceID = ('" . $details_row['raceID'] . "')");
while($rc_row = mysql_fetch_array($racecard))
{
echo "<tr><td>" . $rc_row['number'] . "</td><td>" . $rc_row['past10'] . "</td><td>" . $rc_row['horse_name'] . "</td></tr>";
}
echo "</table><br>";
echo "Testing<br>Testing<br>I wish I could put in some horse form here...<br>";
echo "<table width='990' border='1' cellspacing='2' cellpadding='2'>";
}
NOT TESTED/PSEUDO CODE
"SELECT *
FROM horses AS h,
INNER JOIN race_info AS ri ON race_form.raceID = race_info.raceID
WHERE horse_name IN (
SELECT horse_name
FROM race_form AS srf
WHERE h.horse_name = srf.horse_name
)
AND race_form.raceID = ('" . $details_row['raceID'] . "')"
The idea is to join the two queries into one, I know this is not the correct syntax but it might give you an idea on how to go about it.
Or you can do another query while loop for the horse names
$racedetails = mysql_query("SELECT *
FROM race_info
WHERE meetingID = ('" . $safemeetingID . "')");
while($details_row = mysql_fetch_array($racedetails))
{
echo "<tr><td>Race " . $details_row['race_number'] . " at " . $details_row['start_time'] . " " . $details_row['class'] . " over " . $details_row['distance'] . "m</td></tr>";
$racecard = mysql_query("SELECT *
FROM race_form
INNER JOIN race_info ON race_form.raceID = race_info.raceID
WHERE race_form.raceID = ('" . $details_row['raceID'] . "')");
while($rc_row = mysql_fetch_array($racecard))
{
echo "<tr><td>" . $rc_row['number'] . "</td><td>" . $rc_row['past10'] . "</td><td>" . $rc_row['horse_name'] . "</td></tr>";
$horses = mysql_query("SELECT *
FROM horses
WHERE horse_name = ('" . $rc_row['horse_name'] . "')");
while($horse_row = mysql_fetch_array($horses))
{
// echo horse details here
}
}
echo "</table><br>";
echo "Testing<br>Testing<br>I wish I could put in some horse form here...<br>";
echo "<table width='990' border='1' cellspacing='2' cellpadding='2'>";
}
I theory you could and in practice you can, but a while in a while in a for in a while seems a little bit over the top...
I'm sure we can help you make it more efficient if you explain what it is you're trying to do.