How to Count number of rows in a table that matches to the related condition and echo that count out.
Code goes as follows::
<?php
$sql = "SELECT * FROM input ORDER BY date DESC";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$myid = $row["id"] ;
$sql3 = "SELECT COUNT question_id FROM output WHERE question_id = $myid";
$result3 = $conn->query($sql3);
?>
<div id="q">
<small><p><?php echo $row["date"]; ?></p></small>
<p id="tag3"><small><?php echo $result3['']; ?></small></p>
</div>
Any Suggestions will be appreciated..
Quotes missing
$sql3 = "SELECT * question_id FROM output WHERE question_id = '".$myid."'";
Secondly i dont see closing braces for while
<?php
$sql = "SELECT * FROM input ORDER BY date DESC";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$myid = $row["id"] ;
$sql3 = "SELECT COUNT(*) as rowCount FROM output WHERE question_id = '".$myid."'";
$result3 = $conn->query($sql3);
$rowCount= $result3->fetch_assoc();
?>
<div id="q">
<small><p><?php echo $row["date"]; ?></p></small>
<p id="tag3"><small><?php echo $rowCount['rowCount']; ?></small></p>
</div>
<?php }//while
} //if
?>
You need to use COUNT(question_id) function as below
$sql3 = "SELECT COUNT(question_id ) as TotalQuestions FROM output WHERE question_id = $myid";
$result3 = $conn->query($sql3);
Then to fetch result from above query use fetch_fieled()
while ($info = $result3->fetch_field()) {
$TotalCount = $info->TotalQuestions ;
}
And then display the value
<small><?php echo $TotalCount ; ?></small
try this
$sql3 = "SELECT COUNT(1) as row_count FROM output WHERE question_id = ".$myid."";
$result3 = $conn->query($sql3);
$row_count = $result3->fetch_assoc();
echo $row_count['row_count'];
Related
Is it possible to extract highest id from table (in this case 9) and return it as variable $maximum, which I can use later as integer?
You can use MAX() function. Doc can be found here
Try this, It will must work.
$sql = "SELECT id FROM some_table ORDER BY id DESC LIMIT 1";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "id = " . $row["id"];
}
}
You can try with following snippet:
$sql = "SELECT * FROM some_table where id = (select max(id) from some_table)";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "id = " . $row["id"];
}
}
Try this, It will must work.
$sql = "SELECT id FROM some_table ORDER BY id DESC LIMIT 1";
$result = $conn->query($sql);
$id = 0;
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$id = $row["id"];
}
}
echo $id;
I have case manager table where i have inserted court table id as foreign key. i want to fetch record from both tables. when using nested while loop it shows only one row data.
$id = $_SESSION['id'];
$query1 = "SELECT * from `case_manager` where user_id = '$id' ";
$result1 = mysqli_query($conn, "$query1");
while($row = mysqli_fetch_array($result1, MYSQLI_ASSOC)) {
$Status = $row['status'];
$id = $row['id'];
$case_type = $row['case_type'];
$court_id = $row['court_id'];
$query2 = "SELECT * from `case_type` where case_id = '$case_type'";
if($result1 = mysqli_query($conn, "$query2")) {
while($row2 = mysqli_fetch_array($result1, MYSQLI_ASSOC)) {
echo $row2['case_name'];
}
}
}
Because you are overwritting you $result1 change inner query result to $result2 then try
$id = $_SESSION['id'];
$query1 ="SELECT * from `case_manager` where user_id = '$id' ";
$result1 = mysqli_query($conn , "$query1");
while ($row = mysqli_fetch_array($result1 ,MYSQLI_ASSOC)) {
$Status=$row['status'];
$id = $row['id'];
$case_type = $row['case_type'];
$court_id = $row['court_id'];
$query2 ="SELECT * from `case_type` where case_id = '$case_type'";
if($result2 = mysqli_query($conn , "$query2")){;
while ($row2 = mysqli_fetch_array($result2 ,MYSQLI_ASSOC)) {
echo $row2['case_name'];
}
}
}
1st : Because you are overwriting the variable $result1 In second query execution.
if($result1 = mysqli_query($conn , "$query2")){;
^^^^^^^^ ^^
Note : And remove that unnecessary semicolon .
2nd : No need multiple query simple use join
SELECT cm.*,c.* from `case_manager` cm
join `case_type` c
on cm.cas_type=c.case_id
where cm.user_id=$id;
You can use below query to fetch your record:
$query = SELECT case_manager.* ,case_type.case_name FROM case_manager Left JOIN case_type ON case_manager.case_type=case_type.case_id where case_manger.user_id = $id;
While($row = mysql_fetch_array()){
echo $row['case_name'];
}
GOAL:
Select from the database to target a team name from one of my five specific columns. the table has a number of columns 5 of which are headed respondent_team_1 to respondent_team_5. In each of those columns there maybe a string value that is repeated so the idea is to look at that columns and know this and just take the 1 distinct value and not all so I can match up the data like so:
CODE:
<?php
$sql = "SELECT distinct respondent_team_1 FROM respondent_data WHERE dashboard_id = $user_dash_id";
$result = $conn->query($sql);
if($result->num_rows > 0){
while($row = $result->fetch_assoc()){
echo $row["respondent_team_1"];
}
}
?>
<br>
<?php
$sql = "SELECT distinct respondent_team_2 FROM respondent_data WHERE dashboard_id = $user_dash_id";
$result = $conn->query($sql);
if($result->num_rows > 0){
while($row = $result->fetch_assoc()){
echo $row["respondent_team_2"];
}
}
?>
<br>
<?php
$sql = "SELECT distinct respondent_team_3 FROM respondent_data WHERE dashboard_id = $user_dash_id";
$result = $conn->query($sql);
if($result->num_rows > 0){
while($row = $result->fetch_assoc()){
echo $row["respondent_team_3"];
}
}
?>
<br>
<?php
$sql = "SELECT distinct respondent_team_4 FROM respondent_data WHERE dashboard_id = $user_dash_id";
$result = $conn->query($sql);
if($result->num_rows > 0){
while($row = $result->fetch_assoc()){
echo $row["respondent_team_4"];
}
}
?>
<br>
<?php
$sql = "SELECT distinct respondent_team_5 FROM respondent_data WHERE dashboard_id = $user_dash_id";
$result = $conn->query($sql);
if($result->num_rows > 0){
while($row = $result->fetch_assoc()){
echo $row["respondent_team_5"];
}
}
?>
SUMMARY:
What I am trying to do is refine all those code blocks into one code block as A it is messy and B there must be a better way but I am unable to find one?
I have tried various methods, the obvious being take the column names I need and make a select against them, I tried to use the GROUP BY keyword with no luck either. Im not looking for somebody to do this for me but just some wisdom or advice would be excellent.
Hello here is my current code:
$sqlpack = "Select * from package_in_plan where plan_id='$package_id' order by plan_id";
$planres = mysqli_query($conn,$sqlpack);
$plan = array();
while ($row1 = mysqli_fetch_assoc($planres)){
$plan[] = $row1['package_id'];
}
$plan_1 = implode(',', $plan);
for ($x = 0; $x < count($plan); $x++) {
$sql_service = "Select * from service_in_package where package_id='".$plan[$x]."'";
$chid = mysqli_query($conn,$sql_service);
while ($row = mysqli_fetch_array($chid)){
$ch_id = $row['service_id'];
$sql = "Select * from itv where status='1' and id='$ch_id' order by number asc";
$results = mysqli_query($conn,$sql);
while ($row = mysqli_fetch_array($results)){
$nr = $row['id'];
$namn = $row['name'];
$chnr = $row['number'];
echo $nr;
echo $namn;
echo $chnr;
}
}
}
What i need is the output to be sorted by number($chnr), right now my code is not sorting because it's receiving specific id from previous select ($ch_id).
How can i let the output of $results to be sorted "order by number".
Number is INT in itv table.
You could make use of subqueries which results in a single query that can be sorted.
$sql = "Select * from itv where status='1' and id IN
(Select service_id from service_in_package where package_id IN
(Select package_id from package_in_plan where plan_id='$package_id' order by plan_id))
order by number asc";
$results = mysqli_query($conn,$sql);
while ($row = mysqli_fetch_array($results)){
$nr = $row['id'];
$namn = $row['name'];
$chnr = $row['number'];
echo $nr;
echo $namn;
echo $chnr;
}
How to display 10 latest jobs from database like this. Please click here to see image
I have written following code so far not sure what to do next.
$query = "SELECT * FROM jobs ORDER BY posting_date DESC LIMIT 10";
$query = mysqli_query($conn, $query);
while ($row = mysqli_fetch_assoc($query) ) {
$job_title = row["job_title"];
$company_name = $row["company_name"];
$department = $row["department"];
$location = $row["location"];
$job_type = $row["job_type"];
$job_description = $row["job_description"];
$posting_date = date('d-m-y');
}
The $row returns all the columns from database. I want only four job title, company name, location and date
Use column names in select query.
$query = "SELECT job_title,company_name,location,posting_date FROM jobs ORDER BY posting_date DESC LIMIT 10";
$query = mysqli_query($conn, $query);
echo "<table>";
echo "<tr> <th>Job Title</th> <th>Company Name</th> <th>Location</th> <th>Date Posted</th> </tr>";
echo "<tbody>";
while($row = mysqli_fetch_assoc($query) ) {
$job_title = $row["job_title"];
$company_name = $row["company_name"];
$department = $row["department"];
$location = $row["location"];
$posting_date = date('d-m-y', strtotime($row['posting_date']));
echo "<tr>";
echo "<td>".$job_title."</td>";
echo "<td>".$company_name."</td>";
echo "<td>".$location."</td>";
echo "<td>".$posting_date."</td>";
echo "<tr>";
}
echo "</tbody>";
echo "</table>";
Hmm change the query
$query = "SELECT job_title, company_name, location, posting_date FROM jobs ORDER BY posting_date DESC LIMIT 10";
You can order by for rownum
$query = "SELECT job_title, company_name, location, posting_date FROM jobs ORDER BY rownum DESC LIMIT 10";
Here is an example how to build a htrml table with the 4 columns:
$query = "SELECT * FROM jobs ORDER BY posting_date DESC LIMIT 10";
$query = mysqli_query($conn, $query);
echo "<table>";
while ($row = mysqli_fetch_assoc($query) ) {
echo "<tr>";
echo "<td>$row['job_title']</td>";
echo "<td>$row['company_name']</td>";
echo "<td>$row['location']</td>";
echo "<td>$row['posting_date']</td>";
echo "<tr>";
}
echo "<table>";