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";
Related
/* To sort the id and limit the post by 40 */
$sql = "SELECT * FROM requests";
$result = $conn->query($sql);
$sqlall= "SELECT * FROM requests ";
$resultall = $conn->query($sqlall);
$i = 0;
if ($result->num_rows > 0) {
// Output data of each row
$idarray= array();
while($row = $result->fetch_assoc()) {
echo "<br>";
// Create an array to store the
// id of the blogs
array_push($idarray,$row['id']);
}
}
else {
echo "0 results";
}
?>
<?php
for($x = 1; $x < 40; $x++) {
// This is the loop to display all the stored blog posts
if(isset($x)) {
$query = mysqli_query(
$conn,"SELECT * FROM `requests`");
$res = mysqli_fetch_array($query);
$email1 = $res['email1'];
$msg1= $res['msg1'];
$subject1 = $res['subject1'];
$name1 = $res['name1'];
$id = $res['id'];
the output is 40 cards reading data from the first row in my database. can anyone help?
I'm using xampp.
This code is to show the loop, but if anyone wants the full code is here
You are storing all the IDs in the array $idarray, but then you don't really use them properly. You loop over them, but you just run SELECT * FROM requests` 40 more times, and always extract the same first row. You never use the ID to change the query.
But it really makes no sense to run lots of separate queries anyway. If you just want the first 40 rows then use MySQL's LIMIT keyword. It usually works best when combined with ORDER BY as well. Something like this:
$sql = "SELECT * FROM requests ORDER BY id LIMIT 40";
$result = $conn->query($sql);
while ($res = $result->fetch_assoc()) {
$email1 = $res['email1'];
$msg1 = $res['msg1'];
$subject1 = $res['subject1'];
$name1 = $res['name1'];
$id = $res['id'];
//example output, just for demo:
echo $email1." ".$msg1." ".$subject1." ".$name1." ".$id;
}
Documentation: https://dev.mysql.com/doc/refman/8.0/en/limit-optimization.html
$id = 2;
// query to fetch delayed
$sql = "SELECT * FROM leads WHERE status = ('$id') LIMIT 1";
$obResult = $objConnection->query($sql);
// query to fetch if there is no delayed
$q = "SELECT * FROM leads WHERE status = ('$id') AND later != '1' ORDER BY postnummer LIMIT 1";
$oResult = $objConnection->query($q);
// primary query to run, which makes use of the two above accordingly
$query = "SELECT * FROM leads WHERE status = ('$id') LIMIT 1";
$objResult = $objConnection->query($query);
while ($row = $objResult->fetch_object()) {
if (new DateTime() > $row->delay && $row->delay != '0000-00-00 00:00:00') {
while ($row = $obResult->fetch_object()) {
echo $row->firmanavn;
}
} else {
while ($row = $oResult->fetch_object()) {
echo $row->firmanavn;
}
}
}
Changed my code to this, and still i got the same problem, the if clause if met, but it echoes from my else instead
Reason is that you are using same variable names that overwrite each other. $objResult rename this to something like $objResult2 for the inner query.
One thing to keep in mind is that your inner query inside a loop is really unnecessary unless you did not provide some piece of code. You can just put that query outside of while loop. Will save you time & memory.
Although I think there are other ways this code could be cleaned up, it seems to me that you're attempting to compare a DateTime object to a date string, which is going to yield unpredictable results. Try changing your while part to:
$current_date = new DateTime();
while ($row = $objResult->fetch_object()) {
if (
$current_date->format('Y-m-d H:i:s') > $row->delay &&
$row->delay != '0000-00-00 00:00:00'
) {
while ($row = $obResult->fetch_object()) {
echo $row->firmanavn;
}
} else {
while ($row = $oResult->fetch_object()) {
echo $row->firmanavn;
}
}
}
I'm not sure this will fully solve your issues, but it should get you closer.
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)
This is my code below
$query = mysql_query("SELECT * FROM table ORDER BY id LIMIT 10");
while($f = mysql_fetch_array($query)){
$match = 0; //In reality it's an array search function which returns 1 on match
if($match == 1) {
echo"Show content!";
}
}
Im trying to make an list with 10 rows, and i have a function which uses "name" from table to run an search query with an array generated by twitter API. In example if i get 3 matching records, i still want to show a list with 10 rows but hide the matching elements from there.
At the moment the script hides the matching elements and shows 7 rows instead of 10.
That is what i need help with, cheers :)
Process the data you are getting from the twitter api, collect the data you need in an array and query the database afterwards.
Something like that:
<?php
$collectArray = array();
foreach ($twitterData as $index => $data) {
if ($someCriteria === TRUE) {
$collectArray[] = $data;
}
}
$implodedCollectArray = "'" . implode("', '", $collectArray) . "'";
$sql = "SELECT * FROM `table_name` WHERE `some_column` IN (" . $implodedCollectArray . ")";
$query = mysql_query($sql) or die(mysql_error());
while($f = mysql_fetch_array($query)){
echo $f['column'];
}
?>
reinitilize match variable as the code:
$query = mysql_query("SELECT * FROM table ORDER BY id LIMIT 10");
while($f = mysql_fetch_array($query)){
if($f['name']!='variable_name_twiter'){
$match = 1; //In reality it's an array search function which returns 1 on match
}
if($match == 1) {
echo"Show content!";
}
$match = 0;
}
I am using the following MySQL query to generate a table for users in a database. The query is designed to just return one row for each user, even though there are multiple rows for each user. This works fine, however I also need to calculate the number of unique entries for each user, to enter into the table where it states HERE. Do I need to use another query to return the count for all entries, and if so how do I integrate this with the code I already have?
$query="SELECT from_user, COUNT(*) AS num FROM tracks GROUP BY from_user ORDER BY COUNT(*) DESC";
$result=mysql_query($query) or die(mysql_error());
while ($row = mysql_fetch_array($result)) {
$user = $row['from_user'];
echo "<tr>";
echo "<td>".$user."</td>";
echo "<td>uploads (**HERE**)</td>";
echo "<td>favourites (count)</td>";
echo "</tr>";
}
?>
</table>
Because you've already created the custom field 'num', you can use that to get the count!
Add the following line after user = ...
$count = $row['num'];
Then you can
echo "<td>uploads ($count)</td>";
It miss your table stucture to know your field name, but, if i well understand your question you can use count + distinct in mysql.
You can check this answer too.
SELECT DISTINCT(from_user) AS user,
COUNT(from_user) AS num
FROM tracks
GROUP BY from_user
ORDER BY num DESC";
For the second problem you can doing a second query, or do a join tracks .
I think, in your case it's easier to you to do se second query inside the loop to get all detail from 'user' result.
$query1="SELECT DISTINCT(from_user), COUNT(*) AS num
FROM tracks
GROUP BY from_user
ORDER BY COUNT(*) DESC";
$query2="SELECT * FROM tracks";
$result1=mysql_query($query1) or die(mysql_error());
$result2=mysql_query($query2) or die(mysql_error());
$user_array = array();
while ($row = mysql_fetch_array($result1)) {
$user = $row['from_user'];
$num = $row['num'];
$uploads_array = array();
while ($sub_row = mysql_fetch_array($result2)) {
if( $sub_row['from_user'] == $user ) {
//for example only due to the unknown structure of your table
$uploads_array[] = array(
"file_name" => $sub_row['file_name'],
"file_url" => $sub_row['file_url']
);
}
}
$user_array[] = array(
"name" => $user,
"num_entry" => $num,
"actions" => $uploads_array
);
}
// now the table with all data is stuctured and you can parse it
foreach($user_array as $result) {
$upload_html_link_arr = array();
$user = $result['name'];
$num_entry = $result['num_entry'];
$all_actions_from_user_array = $result['actions'];
foreach($all_actions_from_user_array as $upload) {
$upload_html_link_arr[] = sprintf('%s', $upload["file_url"],$upload["file_name"]);
}
$upload_html_link = implode(', ',$upload_html_link_arr);
$full_row = sprintf("<tr><td>%s</td><td>uploads : %s</td><td>favourites (%d)</td></tr>", $user, $upload_html_link, $num_entry);
// now just echo the full row or store it to a table for the final echo.
echo $full_row;
}
I hope this help, mike