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.
Related
I'm currently trying to link some databases up for a project I'm working on for my business. Here's my below SQL I'm using for my PHP website.
$sql = "SELECT lname, fname, designation, agency, mobile, direct, email FROM contacts";
$result = $conn->query($sql);
At the moment my output goes like this to show what's in each row using e.g. $lname=$lname["variable"]
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
/* Print the row */
echo "<tr>"."<td>". $lname . "</td><td>" . $fname . "</td><td>" . $designation . "</td><td>" . $agency . "</td><td>" . $mobile . "</td><td>" . $direct . "</td><td><a href='mailto:" . $email . "''>". $email. "</a><br>";
}
}
I have another "clients" table, where each agency has an id - one that matches with $agency at this point. What I want to do is post the corresponding companyname column to that id.
How would I modify my code to do so (and access the column/rows from the secondary table)?
If I've understood the question correctly, the answer should look something like
SELECT ct.lname, ct.fname, ct.designation, ct.agency, ct.mobile,
ct.direct, ct.email, cl.company_name
FROM contacts ct
JOIN clients cl ON ct.agency = cl.id
The crux of it is the SQL JOIN clause.
Im sorry for my bad english but this is the best i can.
I am trying to make a script that get's the item values of one line in my
for this example i'll be using the row with ID 2.
The first query is working correctly and getting the 2 values of 1items and 2items and deletes the ; that comes with it.
But the second part seems a little bit harder and i can't solve it.
The query is getting id, base_item from the table
The last 2 Outputs marked red are the one's that are matching 1items & 2items
I'm trying to let the if statement filter the $item1 as id in the item table and output the baseitem of the found row
same goes for item2
I'm using MySQL & MySQLi because this cms doesnt support newer versions of PHP yet.
<?php
$query = "SELECT * FROM logs_client_trade ORDER by id DESC";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){
$item1 = rtrim($row['1items'],"; ");
$item2 = rtrim($row['2items'],"; ");
echo("<tr>");
echo("<td>" . $row['id'] . "</td>");
echo("<td>" . $row['1id'] . "</td>");
echo("<td>" . $row['2id'] . "</td>");
$userinfo = "SELECT id, base_item FROM items LIMIT 1";
$result2 = mysql_query($userinfo) or die(mysql_error());
while($row2 = mysql_fetch_assoc($result2)){
echo("<td>");
if($row2['id'] == $item1){ echo $row2['baseitem']; } else echo "Not available";
echo ("</td>");
echo("<td>");
if($row2['id'] == $item1){ echo $row2['baseitem']; } else echo "Not available";
echo ("</td>");
}
$tradetime = $row['timestamp'];
$date = date("d M Y - H:i:s", $tradetime);
echo("<td>$date</td></tr>");
}
?>
You forgot the while loop for the second query:
$userinfo = mysql_query("SELECT id, base_item FROM items");
while($get2 = $userinfo->fetch_assoc()) {
$baseid = $get2['id'];
$baseitem = $get2['baseitem'];
echo("<tr>");
[...]
}
In addition to that, your code is a mess. In the second loop you are still listing the rows from the logs table... I suggest you to review carefully your code, being careful on how you are using the different $row and $get2 arrays.
You can use a JOIN to get all of the info with one SQL statement:
SELECT *
FROM `logs_client_trade` a
LEFT JOIN `items` b
ON REPLACE(a.`1items`,';','') = b.`id`
LEFT JOIN `items` c
ON REPLACE(a.`2items`,';','') = c.`id`
ORDER by a.`id` DESC
UPDATE:
Here's the PHP code that will run the query and output the table rows. I've specified the column names based on the code in the original question.
<?php
$query = "SELECT
a.`id`,
a.`1id`,
a.`2id`,
a.`1items`,
a.`1items`,
IFNULL(b.`baseitem`,'Not Available') as `baseitem1`,
IFNULL(c.`baseitem`,'Not Available') as `baseitem2`,
DATE_FORMAT(a.`timestamp`,'%d %m %Y - %H:%i:%s') as `tradetime`
FROM `logs_client_trade` a
LEFT JOIN `items` b
ON REPLACE(a.`1items`,';','') = b.`id`
LEFT JOIN `items` c
ON REPLACE(a.`2items`,';','') = c.`id`
ORDER by a.`id` DESC
";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){
$item1 = rtrim($row['1items'],"; ");
$item2 = rtrim($row['2items'],"; ");
echo "<tr>\r\n";
echo " <td>" . $row['id'] . "</td>\r\n";
echo " <td>" . $row['1id'] . "</td>\r\n";
echo " <td>" . $row['2id'] . "</td>\r\n";
echo " <td>" . $row['baseitem1'] . "</td>\r\n";
echo " <td>" . $row['baseitem2'] . "</td>\r\n";
echo " <td>" . $row['tradetime'] . "</td>\r\n";
echo("</tr>\r\n");
}
?>
So i have to have a page where all of the logs from another one of our pages is echoed out.
$query = "SELECT * FROM `returnform` WHERE department_id=1";
$result = mysql_query($query);
while($row = mysql_fetch_array($result)){
echo "<tr class='divEnglish'> <td>" . $row['name'] . "</td>" . " " . "<td>" . $row['returned_date'] . "</td>" . " " . "<td>" . $row['lost'] . "</td>" . " " . "<td>" . $row['lossnum'] . "</td>" . " " . "<td>" . $row['putback'] . "</td>","</tr>";
};
I need all of the rows echoed out to be put in order from newest to oldest so that when people view the page the latest logs.
You need to add order by clause into your query.
$query = "SELECT * FROM `returnform` WHERE department_id=1 ORDER BY create_date DESC";
I suppose you have date, when to record was created, the column name is create_date.
I'm using OpenCart ecommerce, and I would like to see in the index of ecommerce the total number of products
I do not have (for now) access to the db opencart, so to understand what is the structure of the db I have referred to this image
And this is an example of a query that I'm trying to use to display the total number of products (the result is obviously not what I expect)
//Test Count Product
//$query_test = $db->query("SELECT " . DB_PREFIX . "product_description.name FROM " . DB_PREFIX . "product INNER JOIN " . DB_PREFIX . "product_description ON " . DB_PREFIX . "product.product_id = " . DB_PREFIX . "product_description.product_id");
$query_test = $db->query("SELECT * FROM " . DB_PREFIX . "product");
$count_test = 0;
foreach ($query_test as $row) {
$count_test++;
}
echo $count_test;
Try this:
$query = $db->query("SELECT COUNT(*) AS total FROM ".DB_PREFIX."product");
echo $query->row['total'];
Nicolo,
Your code is also correct. For that you need to use this code
$query_test = $db->query("SELECT * FROM " . DB_PREFIX . "product");
$count_test = 0;
foreach ($query_test->rows as $row) {
$count_test++;
}
echo $count_test;
But, I will recommend Monkeyman's way to get product_totals but with some modification (Monkeyman's code will NOT work for multiple stores)
$query = $db->query("SELECT COUNT(*) AS total FROM ".DB_PREFIX."product_to_store WHERE store_id = '" . (int)$this->config->get('config_store_id') . "'");
echo $query->row['total'];
I have two databases same server same domain and same username and password. I need away to select from the main DB and then the second DB I need to be able to connect to it and insert data.
this is currently what I am trying but it is not working.
public function publish(){
$result = mysql_query("SELECT * FROM customer_detail WHERE approvedforsite = 2");
while($row = mysql_fetch_array($result))
{
echo $row['customer_id'] . " - " .$row['TradingName'] . " - " . $row['Phone'] . " - " . $row['Street'] . " - " . $row['City'] . " - " . $row['State'] . " - " . $row['PostCode'] . " - " .$row['Description'];
echo "<br />";
mysql_query("INSERT INTO realcas_incard_server.approved_business (customer_id, tradingname)
VALUES ('". $row['customer_id'] ."','".$row['TradingName']."')");
mysql_query("INSERT INTO realcas_incard_server.business_stores (customer_id, storeid, phone, street, suburb, state, postcode, description)
VALUES ('". $row['customer_id'] ."', 1, '".$row['Phone']."', '" .$row['Street'] . "', '" . $row['City'] . "', '" . $row['State'] . "', '" . $row['PostCode'] . "','".$row['Description']."')");
$offerresult = mysql_query("SELECT * FROM customer_realcash_offer WHERE businessid = ".$row['customer_id']);
while($offers = mysql_fetch_array($offerresult))
{
mysql_query("INSERT INTO _incard_server.Real_Cash_Offers (business_id,storeid,offer) VALUES (".$row['customer_id'].",1,'".$offers['offer']."')");
echo $offers['offer']. "<br/>";
}
echo "<br />";
echo "<br />";
}
}
You can either use mysql_select_db() to switch between databases on the same server/username/password/connection, or you can use the database_name.table_name.column_name dotted syntax (made easier with aliases, as in select a.column from database_name.table_name as a).
Those are your two options. (The third option is to just use one database, but I'm assuming that's not an option)
say there is a table test in database first
test
id int
and table junk in database second
junk
id int
then
query would be
insert into junk(id) (select id from first.test);