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)
Related
It calculates, but starting from the second row.
<?php
include('connect-db.php');
$query = "select * from users";
$result = mysql_query($query);
$row = mysql_fetch_array($result);
$sold= array();
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$sold=$row['contract']+$row['tva'];
echo "<table><tr><td>" . $sold. "</td></tr></table>";
}
?>
Your code has many issues:
Your code starts to calculate from the second row because of the line:
$row = mysql_fetch_array($result);
which obtains the first result from the opened recordset before the while loop.
$sold = array();Why is that an array?
If you want to sum to $sold, threat the variable as an integer and initialize it with a 0.
$sold = 0;
while ($row = mysql_fetch_array($result, MYSQL_ASSOC))
$sold += $row['contract']+$row['tva'];
echo "<table><tr><td>" . $sold. "</td></tr></table>";
It seems to me also that you may want to print the table only once. If this is true, consider to query the database with an aggregation function like SUM():
SELECT SUM(contract + iva) AS contractIva FROM users GROUP BY <some column in your table>;
The above allows to remove the while loop.
Since you already extracted a row from the result, with $row = mysql_fetch_array($result);, the script starts adding only with the next row. Th correct code would be:
<?php
include('connect-db.php');
$query = "select * from users";
$result = mysql_query($query);
$sold= array();
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$sold=$row['contract']+$row['tva'];
echo "<table><tr>
<td>" . $sold. "</td>
</tr></table>";
}
?>
you can do that via query as well so that you don't need to perform calculation on the application level, database level can do this job for you.
select sum(col1+col2) as total from users
And you want one table instead of multiple tables I guess, if yes then do it like this:
echo "<table>
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$sold=$row['contract']+$row['tva'];
echo <tr><td>" . $sold. "</td></tr>";
}
echo "</table>";
How to display only one row at random at the same time from DB. Everything works fine, but all rows are displayed. thanks
<?php
$sql = "SELECT id,name FROM table ";
$rows = array();
$result = $objCon->query($sql);
while($row = $result->fetch_assoc())
{
$rows[] = $row;
}
shuffle($rows);
echo '<ol>';
foreach($rows as $row)
{
echo '<li><h3>'.$row['id'].' = '.$row['name'].'</h3></li>';
}
echo '</ol>';
?>
Change your SQL request:
SELECT id,name FROM table ORDER BY RAND() LIMIT 1;
You can do it using PHP:
....
shuffle($rows);
$randomRow = reset($rows);
....
But the better way is to change your SQL query:
$query = "SELECT id, name FROM table ORDER BY RAND() LIMIT 1;"
<?php
$sql = "
SELECT id, name
FROM table
ORDER BY RAND()
LIMIT 1 ";
$result = mysql_query($sql);
// As you are only return a single row you do you require the while()
$row = mysql_fetch_array($result);
echo '<ol>';
echo '<li><h3>'.$row['id'].' = '.$row['name'].'</h3></li>';
echo '</ol>';
?>
By adding an ORDER BY RAND() in your sql query you are asking MySQL to randomly order the results then at a LIMIT to restrict the number of rows you would like returned.
The example code is written based on selecting a single row. If you would like more, e.g. 5, you will need to add a while loop.
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";
I am trying to get the total actions from each employee in a MySQL database. My script is giving me a significantly lower total than the number in the database for all employees. I am using a SUM function in the query. The query works fine in phpmyadmin, but not in my script. Any ideas why this is happening.
$query = "SELECT user_id, SUM(num_actions) as action FROM pro_actions GROUP BY user_id ORDER BY action DESC";
if ($result = $db->query($query)) {
$count = 0; // this is the total of all employee actions. which adds up correctly!
while ($row = mysqli_fetch_array($result)) {
$count += $row['action'];
echo '<tr><td>';
echo $row['user_id'];
echo '</td><td>';
echo $row['action'];
echo '</td></tr>';
}
$result->free();
}
When I run this script, employee 1005 has 63 actions. However, when I run this query in phpmyadmin, employee 1005 has 194 actions (which is correct). All employees have fewer actions in the output of the script. The interesting thing is that the $count variable outputs the correct amount, which is the total of all actions... Please help with this glitch.
$query = "SELECT user_id, SUM(num_actions) as action FROM pro_actions GROUP BY user_id ORDER BY action DESC";
if ($result = $db->query($query)) {
$count = 0; // this is the total of all employee actions. which adds up correctly!
while ($row = mysqli_fetch_array($result)) {
$count += $row['action'];
echo '<tr><td>';
echo $row['user_id'];
echo '</td><td>';
echo $row['action'];
echo '</td></tr>'; //tag mismatch
}
$result->free();
}
I'm a bit of a newb to PHP and MySQL. I seem to be having an issue with something. How do I loop through an array, querying each value in the array until the query meets a certain condition.. In this case it would be that the number of rows returned from the query is less than five. Here is what I have:
$query1="SELECT UserID FROM Users where RefID='$userid'";
$result1=mysql_query($query1);
while ($row = mysql_fetch_array($result1, MYSQL_NUM) && $sql2querynum < '5')
{
echo ($row[0]);
echo "
";
$sql2 = "SELECT * FROM Users WHERE RefID=$row[0]";
$sql2result = mysql_query($sql2);
$sql2querynum = mysql_numrows($sql2result);
}
Problem is, for every value it echoes out, I get the following warning:
mysql_numrows(): supplied argument is not a valid MySQL result resource
Like I said, I'm a newb, so maybe I'm not even going about doing this the right way.
try this
$query1="SELECT UserID FROM Users where RefID='$userid'";
$result1=mysql_query($query1);
if(mysql_num_rows($result1)<5)
{
while ($row = mysql_fetch_array($result1))
{
echo ($row[0]);
echo "
";
$sql2 = "SELECT * FROM Users WHERE RefID=$row[0]";
$sql2result = mysql_query($sql2);
$sql2querynum = mysql_numrows($sql2result);
}
}
$query1="SELECT UserID FROM Users where RefID='$userid'";
$result1=mysql_query($query1);
while ($row = mysql_fetch_array($result1, MYSQL_NUM) && $sql2querynum < '5')
{
echo ($row[0]);
echo "
";
$sql2 = "SELECT * FROM Users WHERE RefID={$row[0]}";
$sql2result = mysql_query($sql2);
$sql2querynum = mysql_numrows($sql2result);
}
Use { } for variables in " " ... and why you are not using joins ?