I need to populate an html SELECT OPTION using a while loop with an sequence from 1 to the value of a integer number that is stored in a field called dummy3 inside a table from mysql database.
Example: if the dummy3 field has number 4, than the select option should display 1 2 3 4 as option values.
Here is my code, that is not working. The option values in this code version always come empty (with other code variants i tried the browser looped forever).
Here is my code:
echo 'Quantidade: '.'<select name="product_qty" id="product_qty" class="btn">';
$qt = 1;
$quantidade = "SELECT dummy3 FROM shop_products ORDER BY category ASC";
$quantidade1 = mysqli_query($conn, $quantidade) or die("database error:". mysqli_error($conn));
while($row_quantidade = mysqli_fetch_assoc($quantidade1, MYSQL_NUM)){
$row_qt = $row_quantidade['dummy3'];
while($qt <= $row_quantidade['dummy3']){
echo '<option value="'.$qt.'">'.$qt.'</option>';
}
}
echo '</select>';
echo 'Quantidade: '.'<select name="product_qty" id="product_qty" class="btn">';
$qt = 1;
$quantidade = "SELECT dummy3 FROM shop_products ORDER BY category ASC";
$quantidade1 = mysqli_query($conn, $quantidade) or die("database error:". mysqli_error($conn));
while($row_quantidade = mysqli_fetch_assoc($quantidade1, MYSQL_NUM)){
$row_qt = $row_quantidade['dummy3'];
for($i = 1; $i <= $row_qt; $i++) {
echo '<option value="'.$i.'">'.$i.'</option>';
}
}
echo '</select>';
Does this work?
Related
I have a page in which I want to display "how many" from values in my database. I've attached a picture of what I have so far.
From my current count I know I have 13 transportation items, 2 communications items, etc., so I'm wondering how to output that properly.
<?php
$countSQL = "SELECT username, primary_function, COUNT(*)
FROM resource
WHERE primary_function
IN ('transportation', 'communications', 'engineering', 'search and rescue', 'education', 'energy', 'firefighting', 'human services')
GROUP BY username, primary_function";
$countresult = mysqli_query($conn, $countSQL);
$countdata= mysqli_num_rows($countresult);
$sql = "SELECT pf_id, primary_function FROM primary_function ORDER BY pf_id;";
$result = mysqli_query($conn, $sql);
$queryResult = mysqli_num_rows($result);
if ($queryResult > 0){
while ($row = mysqli_fetch_assoc($result)) {
echo "<tr><td>". $row['pf_id'] ."</td><td>". $row['primary_function'] ."</td><td>". $countdata."</td></td>";
}
}
?>
Use an alias for your COUNT(*), ie resourceCount
SELECT username, primary_function, COUNT(*) as resourceCount
then create an array with the primary_function and resourceCount
$resourceCounts = array();
while ($rowCounts = mysqli_fetch_assoc($countresult)) {
$resourceCounts['primary_function'] = $rowCounts['resourceCount'];
}
Then in your table, use the $row['primary_function'] to get the resourceCount -> $resourceCounts[$row['primary_function']]
echo "<tr><td>". $row['pf_id'] ."</td><td>". $row['primary_function'] ."</td><td>". $resourceCounts[$row['primary_function']] ."</td></td>";
To get your final total, create a counter var and add that value to it -
$total = 0;
....
// add the value in the loop
$total += $resourceCounts[$row['primary_function']];
....
I'm going to count the sum of the value in rmNum column and with the same value of rmType.
From the table as shown in image above, can someone please teach how to to sum up the value of rmNum by referring to the same "keyword" of rmType?
i am new to PHP and i still get confusing about this even i went through the teaching part in other websites and questions that asked in Stackoverflow.
Edit 2:
<?php
require_once 'dbconnect.php';
$sql = "SELECT * FROM room";
$result = mysqli_query($conn, $sql);
echo $conn->error;
while($row = mysqli_fetch_array($result)) {
$typeRow = $row;
}
$sql1 = "SELECT rmType, SUM(rmNum) as total FROM room GROUP BY rmType";
$result = mysqli_query($conn,$sql1);
while ($row1 = mysqli_fetch_assoc($result)) {
echo $row1['rmType'] . ' had ' . $row1['total'] . ' room reservations';
}
mysqli_close($conn);
?>
this is the coding i used after all, it still give me Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean
You can use a GROUP BY statement:
$sql = "SELECT rmType, SUM(rmNum) as total FROM table GROUP BY rmType";
where table is the name of the table. This will return a table with two columns: the rmType and the total sum of rmNum for that type.
You can then process it like:
$sql = "SELECT rmType, SUM(rmNum) as total FROM table GROUP BY rmType";
$result = $mysqli->query($sql);
while ($row = $result->fetch_assoc()) {
echo $row['rmType'] . ' had ' . $row['total'] . ' room reservations';
}
EDIT: In case you want the total sum, you do not need to group, you can simply use:
$sql = "SELECT SUM(rmNum) as total FROM table";
$result = $mysqli->query($sql);
if ($row = $result->fetch_assoc()) {
echo 'The total number of reservation rooms is ' . $row['total'];
}
I want to display data from all rows only for the column named 'text_notes' from my db.
And I want to loop that data in a p tag so that all data from column text_notes is displayed one by one till the end.
Below is the code I tried but it displays all the columns of the latest row and doesn't display the other rows.
I want the data from column 'text_notes' from all the rows to be displayed from latest at the bottom to the oldest at the top.
<?php
$query = "SELECT * FROM notes";
$conn = new mysqli(DBROOT,DBUSER,DBPASS);
$conn->select_db(DBNAME);
if ($conn == TRUE) {
$runQuery = $conn->query($query);
$resultNum = $runQuery->num_rows;
$result = $runQuery->fetch_array();
$resultRow = mysqli_fetch_row($runQuery);
$notes = $result['text_notes'];
for ($i = 0; $i < $resultNum;) {
echo "<p>".$resultRow[$i]."</p>";
$i++;
}
}
?>
$mysqli = new mysqli(DBROOT, DBUSER, DBPASS, DBNAME);
$query = "SELECT text_notes FROM notes";
$result = $mysqli->query($query, MYSQLI_STORE_RESULT);
// Cycle through the result set
$counter = 0;
while(list($text_notes) = $result->fetch_row())
echo "<p>";
$counter++;
echo $counter . " : ";
echo " $text_notes </p>";
// Free the result set
$result->free();
I think it works :) Thanks
This code below echoes out 8 names from tableOne, but I want to compare those names with 8 names in another table. I want to compare the rows echoed in $row['weight'] with tableTwo, and if the results don't match, then add a <span class="strike"> </span> to the result echoed in $row['weight'].
How do I go about adding an if/else to $row['weight'] compare each name with the names in another table?
$result = mysqli_query($con,"SELECT * FROM tableOne LIMIT 0, 8");
$i = 1;
while($row = mysqli_fetch_array($result)) {
echo $i. " - " . $row['weight'] . '<br>';
$i++;
}
Here is some simple code to get you started:
$result = mysqli_query($con,"SELECT * FROM tableOne LIMIT 0, 8");
$result2 = mysqli_query($con,"SELECT * FROM tableTwo LIMIT 0, 8");
$i = 1;
while($row = mysqli_fetch_array($result)) {
$value1 = $row['weight'];
$row2 = mysqli_fetch_array($result2);
$value2 = $row2['weight'];
echo $i . " - table 1: ";
echo $value1;
echo ", table 2: - ";
if ($value2 != $value1) {
echo '<span class="strike">$value2</span>';
} else {
echo $value2;
}
echo '<br>';
$i++;
}
You can make the code smarter, to handle cases where there aren't 8 values to compare, and to display the values in an HTML table too, but hopefully this can get you started.
Try this:
$sql="select * from tableone to left join tabletwo tw on to.weight=tw.weight ";
This query will return all the rows in tableone which matches and empty rows for the ones where the weight dont match.
So in your php code:
while($row = mysqli_fetch_array($result)) {
if(empty($row['weight'])){
echo '<span class="strike">$row[weight]</span>';
}
}
This would work with any dynamic number of records in the tables.
How about keeping the values ($row[]) in one array($tableOne) and doing the same thing for table two ($tableTwo) and then perform your comparison in foreach()? (For the sake of simplicity, if you don't want any joins)
I have 2 tables with a unique ID. I have a table with one of the columns being a date field. I am attempting to filter the field by today's date (which it works) from all the rows with "todays" date I want to get the cell info for #key. Once I have that ID, I want to match it with #headkey. So $headkey == $key. Once I filter that, I want to see if any of the fields match = Delivery for the ItemID column. For some reason I have an infinite loop. I played around with the logic but can't seem to get it to work. Any ideas?
$TransactionSql = "SELECT * FROM apcshead WHERE DateInvoiced > 0 ORDER BY DateInvoiced DESC";
$ItemsSql = "SELECT * FROM apcsitem";
$rs=odbc_exec($conn,$TransactionSql);
while($row = odbc_fetch_array($rs))
{
//Grabbing Transaction info
$DateInvoiced = odbc_result($rs,"DateInvoiced");
$ApcsheadKey = odbc_result($rs,"Key");
$DateInvoiced = new DateTime($DateInvoiced);
$DateInvoiced_date = $DateInvoiced->format('m-d-Y');
//$TimeStamp_time = $TimeStamp->format('h:i:s');
if ($DateInvoiced_date == $today)
{
$ItemsRs=odbc_exec($conn,$ItemsSql);
while($row = odbc_fetch_array($ItemsRs))
{
$HeadKey = odbc_result($ItemsRs,"HeadKey");
$ItemID = odbc_result($ItemsRs,"ItemID");
if ($ItemID == 'Delivery')
{
echo 'Delivery';
echo '<br />';
}
}
}
}
*UPDATE:*I modified the code again. Now what if does is it spits out 1 row with the date and then like 100 echo Delivery and then goes back and spits out another date and the same thing. Still not sure what is going on.
$TransactionSql = "SELECT * FROM apcshead WHERE DateInvoiced > 0 ORDER BY DateInvoiced DESC";
$ItemsSql = "SELECT * FROM apcsitem";
$rs=odbc_exec($conn,$TransactionSql);
while($row = odbc_fetch_array($rs))
{
$DateInvoiced = odbc_result($rs,"DateInvoiced");
$DateInvoiced = new DateTime($DateInvoiced);
$DateInvoiced_date = $DateInvoiced->format('m-d-Y');
echo $DateInvoiced_date;
echo '<br />';
if ($DateInvoiced_date == $Today)
{
echo $DateInvoiced_date;
echo '<br />';
$ItemsRs=odbc_exec($conn,$ItemsSql);
while($row = odbc_fetch_array($ItemsRs))
{
$ItemID = odbc_result($ItemsRs,"ItemID");
if ($ItemID == 'Delivery')
{
echo 'Delivery';
}
}
}
}
I solved the issue using the INNER JOIN command. It is great because I don't have to do nested loops :)
Learned how to do it from W3Schools. http://www.w3schools.com/sql/sql_join_inner.asp
This is my current SQL Statement:
$TransactionSql = "SELECT apcshead.Key, apcshead.DateInvoiced, apcshead.InvNum, apcsitem.Headkey, apcsitem.ItemID FROM apcshead INNER JOIN apcsitem ON apcshead.Key=apcsitem.Headkey";