I have database which stores an activity type (swimming, running, soccer, tennis and basketball)under task_cstm
.
Another database stores salesman name
, date_start
and date_due
stored in tasks.
When I run on phpMyAdmin,
SELECT COUNT( tasks_cstm.activity_type_c ) FROM tasks_cstm
LEFT JOIN tasks ON tasks.id = tasks_cstm.id_c
where tasks_cstm.activity_type_c ="swimming" and assigned_user_id="abcdefg"
I got the result that I want:
However I place this into a PHP file which allows the user to select the SALESMAN
and DAT
to see how many activities being promoted by the salesman during the period nothing being shown:
$result = $GLOBALS['db']->query("SELECT tasks_cstm.activity_type_c,
COUNT( tasks_cstm.activity_type_c ) FROM tasks_cstm tc
LEFT JOIN tasks t ON t.id = tc.id_c
WHERE activity_type_c='swimming' AND date_start>='$st'
AND date_due<='$dt' AND assigned_user_id='$salesman' AND t.id=tc.id_c")
echo "<tr><td>".$result."</td></tr>";
Could someone please help me and give me some guidance?
Assuming you are using mysqli_*
extensions, please try this:
$result = $GLOBALS['db']->query("SELECT tasks_cstm.activity_type_c,
COUNT( tasks_cstm.activity_type_c ) FROM tasks_cstm tc
LEFT JOIN tasks t ON t.id = tc.id_c
WHERE activity_type_c='swimming' AND date_start>='$st'
AND date_due<='$dt' AND assigned_user_id='$salesman' AND t.id=tc.id_c")
while ($row = $result->fetch_array(MYSQLI_ASSOC))
echo '<pre>';
print_r($row);
echo '</pre>';
}
The key issue I see from what you have presented you are jumping directly from the $result
to just echoing the $result
instead of running something like $result->fetch_array(MYSQLI_ASSOC)
to actually parse the values.
But it’s still unclear what you expect this to be:
echo "<tr><td>".$result."</td></tr>";
Should that be one row of data? If so, which piece of data? Or are those <tr><td>
and </td></tr>
just a per data holder? If so maybe this would work for one item per row:
while ($row = $result->fetch_array(MYSQLI_ASSOC))
echo '<tr><td>';
echo $row['tasks_cstm.activity_type_c'];
echo '</td></tr>';
}
Or maybe this which sets a table cell for each item within a larger row:
echo '<tr>';
while ($row = $result->fetch_array(MYSQLI_ASSOC))
echo '<td>';
echo $row['tasks_cstm.activity_type_c'];
echo '</td>';
}
echo '</tr>';
Try this
$result = $GLOBALS['db']->query("SELECT tasks_cstm.activity_type_c,
COUNT( tasks_cstm.activity_type_c ) AS totalCount FROM tasks_cstm tc
LEFT JOIN tasks t ON t.id = tc.id_c
WHERE activity_type_c='swimming' AND date_start>='$st'
AND date_due<='$dt' AND assigned_user_id='$salesman' AND t.id=tc.id_c")
echo "<tr><td>".print_r($result)."</td></tr>";
Have you get All Row data in $result if yes then write below code you will get result row data while($ row = mysqli_fetch_array($result)) { echo $ row[0] . " " . $row[1];echo "
";} and If you dont get data in $result then first check connection and then execute query.
Thank you guys for all your help. i got the result that i wanted.
What i did was to add to ...SELECT tasks_cstm.activity_type_c, COUNT( tasks_cstm.activity_type_c ) AS TOTALCOUNT(0-4) for each result.
and for every single result(0-4) i need to have another fetchbyassoc so i input
$r0= $GLOBALS['db']->fetchByAssoc($result0);
$r1= $GLOBALS['db']->fetchByAssoc($result1);
$r2= $GLOBALS['db']->fetchByAssoc($result2);
$r3= $GLOBALS['db']->fetchByAssoc($result3);
$r4= $GLOBALS['db']->fetchByAssoc($result4);
and also i echo out using this
echo "<tr>
<td>".$r0['totalCount0']."</td>
<td>".$r1['totalCount1']."</td>
<td>".$r2['totalCount2']."</td>
<td>".$r3['totalCount3']."</td>
<td>".$r4['totalCount4']."</td>
</tr>";
and yes i got the answer i need. THANK YOU SO MUCH FOR ALL UR HELP (^3^)