I am currently trying to do a monthly timetable.
and this is my coding 'table.php'
<table class="table table-striped table-bordered table-hover" id="dataTables-example">
<thead>
<tr>
<th>Date</th>
<th>8.00AM</th>
<th>9.00AM</th>
<th>10.00AM</th>
<th>11.00AM</th>
<th>12.00PM</th>
<th>1.00PM</th>
<th>2.00PM</th>
<th>3.00PM</th>
</tr>
</thead>
<tbody>
<tr class="odd gradeX">
<?php
$datequery="SELECT date FROM day ";
$resultdate = mysqli_query($con,$datequery);
while($row = mysqli_fetch_assoc($resultdate)) {
?>
<td><?php echo$row['date'] ;?></td>
<?php
$query="SELECT c_name,l_name,time FROM timetable
";
$result = mysqli_query($con,$query);
while($col = mysqli_fetch_assoc($result)) {
?>
<?php
if($_GET['time'==8]){?>
<td><?php echo $col["c_name"]; echo "<br>"; $col["l_name"];?></td>
<?php }?>
<?php
if($_GET['time'==9]){?>
<td><?php echo $col["c_name"]; echo "<br>"; $col["l_name"];?></td>
<?php }?>
<?php
if($_GET['time'==10]){?>
<td><?php echo $col["c_name"]; echo "<br>"; $col["l_name"];?></td>
<?php }?>
<?php
if($_GET['time'==11]){?>
<td><?php echo $col["c_name"]; echo "<br>"; $col["l_name"];?></td>
<?php }?>
<?php
if($_GET['time'==12]){?>
<td><?php echo $col["c_name"]; echo "<br>"; $col["l_name"];?></td>
<?php }?>
<?php
if($_GET['time'==13]){?>
<td><?php echo $col["c_name"]; echo "<br>"; $col["l_name"];?></td>
<?php }?>
<?php
if($_GET['time'==14]){?>
<td><?php echo $col["c_name"]; echo "<br>"; $col["l_name"];?></td>
<?php }?>
<?php
if($_GET['time'==15]){?>
<td><?php echo $col["c_name"]; echo "<br>"; $col["l_name"];?></td>
<?php }?>
<?php }?>
</tr>
<?php }?>
</tbody>
</table>
The problem now is that I cannot display 'c_name' and 'l_name' based on 'time'.
So I need an idea on how can I arrange it according to time.
Like already say Dan, you have an error with your condition with the time field
if($_GET['time' == 8])
this code will compare the string 'time' with the number 8 and return False. So your code will search the key False in your array. The good condition will be:
if($_GET['time'] == 8)
But you can avoid to check all value of the field time with a "where" clause in your SQL query. Instead make:
$query="SELECT c_name,l_name,time FROM timetable";
You can do something like this:
<?php
$query = mysqli_prepare($con, "SELECT c_name,l_name FROM timetable WHERE time = ?;");
//Here you 'replace' the '?' caracter by the value of the variable $_GET['time']
mysqli_stmt_bind_param($query, "d", $_GET['time']);
mysqli_stmt_execute($query);
mysqli_stmt_bind_result($query, $c_name, $l_name);
//here we fetch all returned rows by the sql query and display a line for each of them
while(mysqli_stmt_fetch($query)){
echo "<td> ".$c_name." <br> ".$l_name."</td>";
}
mysqli_stmt_close($query);
?>
Good practice would you sanitize your $_GET['time'] before binding it as a parameter for avoid SQL injection.
This will avoid to have many "if" for found the good entry. Your database will found more quickly the good entry than php.
For see more for prepare statement : http://php.net/manual/fr/mysqli.prepare.php
And for sanatize your parameter:
http://php.net/manual/fr/mysqli.real-escape-string.php
Hope this will help you.
Shouldn't those if statements look more like
if($_GET['time'] == 8) {
// do something
}
?
Related
I have a number of rows (ingredients) in a table in my database, I'm using the following code to iterate and populate my view with the name of each, but I want to be able to display the children/parents (columns in the DB) of each item, I'm able to log out the values correctly but when I try to echo as below I'm receiving "Message: Undefined index: parents" & "Message: Undefined index: children";
<?php foreach ($ingredient as $row => $key) { ?>
<tr>
<td><?php echo $row + 1; ?> </td>
<td><?php echo $key['name']; ?> </td>
<?php ChromePhp::log($key['parents']); ?>
<td><?php echo $key['parents']; ?> </td>
<?php ChromePhp::log($key['children']); ?>
<td><?php echo $key['children']; ?> </td>
<td>
</tr>
<?php } ?>
I'm confused as I have no issue logging it out.
I assumed it may be due to null values so I assigned child/parent a string value for each row as follows;
but the error persists.
Here is the logged output in the browser;
Output of <?php ChromePhp::log($key); ?>
EDIT: Here is how I'm building the $ingredients array;
<?php
function get_ingredient()
{
$qry = "SELECT CONCAT(UPPER(LEFT(i.name, 1)), LCASE(SUBSTRING(`name`, 2))) as name,i.id as id,'ingredient' as type, i.parents as parents, i.children as children FROM `ingredient` i WHERE i.is_del=0 order by i.name asc";
$qry = $this->db->query($qry);
if ($qry->num_rows() > 0) {
$ingr = $qry->result_array();
} else {
$ingr = array();
}
}
?>
I assume I'm missing something fundamental, any advise would be most appreciated. Cheers.
My array had two sets of data, I have resolved this by limiting the array to one set.
<?php foreach ($ingredient as $row => $key) { ?>
<tr>
<td><?php echo $row + 1; ?> </td>
<?php if(isset($key['name']) && !empty($key['name'])):?>
<td><?php echo $key['name']; ?> </td>
<?php endif;?>
<?php if(isset($key['parents']) && !empty($key['parents'])):?>
<?php ChromePhp::log($key['parents']); ?>
<td><?php echo $key['parents']; ?> </td>
<?php endif;?>
<?php if(isset($key['children']) && !empty($key['children'])):?>
<?php ChromePhp::log($key['children']); ?>
<td><?php echo $key['children']; ?> </td>
<?php endif;?>
</tr>
$key looks like an object so you have to access it with -> so to get value of name use $key->name,$key->parents
Im setting up a html table that connected from the database. But I want the table design like the excel which in html table using rowspan. but I cant work out the problem which Im working out.
Please help me to create this format. See the code below
<tbody>
<?php
include_once('connection.php');
$sql = "SELECT * FROM rqn ";
$query = mysqli_query($conn, $sql);
while($row = mysqli_fetch_assoc($query)){
$result = $row['rqn_no'];
?>
<tr>
<td rowspan="2"><?php echo $row['rqn_no'] ?></td>
<td><?php echo $row['date_filed'] ?></td>
<td><?php echo $row['desc_text'] ?></td>
<td><?php echo $row['remarks'] ?></td>
<td><?php echo $row['date_approved'] ?></td>
<td>
<?php
include_once('connection.php');
$sql1 = "SELECT * FROM po_data where po_data.rqn_base = $result ";
$query1 = mysqli_query($conn, $sql1);
while($row1 = mysqli_fetch_assoc($query1)){
?>
<?php echo $row1['po_no'] ?>
<?php
}
?>
</td>
</tr>
<?php
}
?>
</tbody>
Result I Wanted:
https://prnt.sc/md2t2s
Actual Result:https://prnt.sc/md2tpk
You can use rowspan with a div to achieve the result you want. Try this,
<tbody>
<?php
include_once('connection.php');
$sql = "SELECT * FROM rqn ";
$query = mysqli_query($conn, $sql);
while($row = mysqli_fetch_assoc($query)){
$result = $row['rqn_no'];
?>
<tr>
<td rowspan="2"><?php echo $row['rqn_no'] ?></td>
<td><?php echo $row['date_filed'] ?></td>
<td><?php echo $row['desc_text'] ?></td>
<td><?php echo $row['remarks'] ?></td>
<td><?php echo $row['date_approved'] ?></td>
<?php
include_once('connection.php');
$sql1 = "SELECT * FROM po_data where po_data.rqn_base = $result ";
$query1 = mysqli_query($conn, $sql1);
$total_rows = mysqli_num_rows($query1);
// open <td>
echo "<td rowspan=$total_rows>";
while($row1 = mysqli_fetch_assoc($query1)){
?>
<?php echo "<div style='border-bottom:1px solid black'>" . $row1['po_no'] ."</div>" ?>
<?php
}
// close </td>
echo "</td>";
?>
</td>
</tr>
<?php
}
?>
</tbody>
Adjust the CSS according to your need.
Hi this is the final answer to my result.
Output
<tbody>
<?php
include ("connection.php");
$sql = "SELECT * FROM rqn ";
$result= mysqli_query($conn, $sql);
while($row=mysqli_fetch_array($result)):
$ename = $row['rqn_no'];
// count the esal in each ename
$sql2 = "SELECT * FROM po_data WHERE rqn_base=$ename";
$result2 = mysqli_query($conn, $sql2);
$count_result2 = mysqli_num_rows($result2);
?>
<tr >
<td rowspan="<?php echo $count_result2; ?>"><?php echo $row['rqn_no']; ?></td>
<td rowspan="<?php echo $count_result2; ?>"><?php echo $row['date_filed']; ?></td>
<td rowspan="<?php echo $count_result2; ?>"><?php echo $row['desc_text']; ?></td>
<td rowspan="<?php echo $count_result2; ?>"><?php echo $row['remarks']; ?></td>
<td rowspan="<?php echo $count_result2; ?>"><?php echo $row['date_approved']; ?></td>
<?php
// loop each esal
while($row2 = mysqli_fetch_array($result2)):
?>
<td><?php echo $row2['po_no']; ?></td>
</tr>
<?php
endwhile; // endwhile for each esal looping
endwhile; // endwhile for the main looping
?>
</tbody>
Tnx for helping me guys
This question already has answers here:
MySQLi query returns only one row
(3 answers)
Closed 5 years ago.
So I had some mysql code that I've begun to rewrite into mysqli and have run into a problem with the query, and that is when I execute it, I only receive one set of results instead of the several that I know it should be. This is the new code I am using and was wondering whether anyone had any ideas on where I'm going wrong?
code:
<?php
if ($result = $link->query("SELECT SUM(step_count.steps) as total, leagues.league_id, leagues.league_name
FROM step_count INNER JOIN logins on step_count.unique_id = logins.unique_id INNER JOIN leagues ON leagues.unique_id = logins.unique_id GROUP BY leagues.league_id, leagues.league_name ORDER BY `total`
DESC LIMIT 100 ", MYSQLI_USE_RESULT))
$rank = 1; {
$row = $result->fetch_assoc();
$result->close();
}
?>
<tr>
<td>
<?php echo $rank++; ?>
</td>
<td>
<?php echo $row['league_name']; ?>
</td>
<td>
<?php echo $row['total']; ?>
</td>
</tr>
</table>
<?php
mysqli_close($link);
?>
you have to use a while loop
while($row = $result->fetch_assoc()){ ?>
<tr>
<td><?php echo $rank++; ?></td>
<td><?php echo $row['league_name']; ?></td>
<td><?php echo $row['total']; ?></td>
</tr>
<?php } ?>
try like this.
while($row = $result->fetch_assoc()){ ?>
<tr>
<td><?php echo $rank++; ?></td>
<td><?php echo $row->league_name; ?></td>
<td><?php echo $row->total; ?></td>
</tr>
<?php } ?>
you have to put a loop there.
you can replace this code and it will work
while($row =$result->fetch_assoc()){
?>
<tr>
<td><?php echo $rank++; ?></td>
<td><?php echo $row['league_name']; ?></td>
<td><?php echo $row['total']; ?></td>
</tr>
<?php }
$result->close();
}
?>
Fetch assoc retrieves one row as an associative array.
So you must use a while loop to keep fetching the rows until there are no more. The first example clearly illustrates how. I modified your whole code so you can copy paste everything. Do read the example though.
<?php
$query = "SELECT SUM(step_count.steps) as total,
leagues.league_id, leagues.league_name
FROM step_count
INNER JOIN logins on
step_count.unique_id=logins.unique_id
INNER JOIN leagues ON
leagues.unique_id=logins.unique_id
GROUP BY leagues.league_id, leagues.league_name
ORDER BY `total` DESC LIMIT 100";
$rank = 1;
if ($result = $link->query($query, MYSQLI_USE_RESULT)) {
while($row =$result->fetch_assoc()){
?>
<tr>
<td><?php echo $rank++; ?></td>
<td><?php echo $row['league_name']; ?></td>
<td><?php echo $row['total']; ?></td>
</tr>
<?php
}?>
</table>
<?php
}
mysqli_close($link);
I have an activity table that contain the list of activity student had joined before. So if the student is new student, there will have been no activity for that student.
<table align="center" width="1000" border="1" >
<h3>Activity List</h3>
</br>
<tr align="center" style="font-weight:bold" >
<td>ID</td>
<td>Activity</td>
<td>Sem</td>
<td>Session</td>
<td>Achievement</td>
<td>Level</td>
</tr>
<?php do { ?>
<tr align="center">
<td><?php echo $row_Recordset1['student_id']; ?></td>
<td><?php echo $row_Recordset1['activity']; ?></td>
<td><?php echo $row_Recordset1['sem']; ?></td>
<td><?php echo $row_Recordset1['session']; ?></td>
<td><?php echo $row_Recordset1['achievement']; ?></td>
<td><?php echo $row_Recordset1['level']; ?></td>
</tr>
<?php } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>
</table>
How can I make this table only show on screen if it is not empty. Btw, im using session to display the exist record.
The best way to do this would be to get your array via mysql(i)_fetch_array() of the query you have build and then to chec to see if the query has rows like:
$qry = "SELECT `this` FROM `table`"
while ($result = mysql_fetch_array($qry)) {
if (mysql_num_rows($result) > 0) {
echo "We have rows!";
}
else {
echo "Looks like we haven't got anything here!";
}
}
I hope this helps.
Might also help to look here: PHP mysql_num_rows method.
<?php if(!empty($activity))
{
your msg ..
}
else
{
}
?>
where empty() will check a given variable is empty or not
Well that's what the if statement is used for:
if(!count($activities)) {
echo "This student has no activities yet.";
} else {
//display activities
....
}
I have a php table that displays the days of the month in a table:
//count up the days, untill we've done all of them in the month
while ( $day_num <= $days_in_month )
{
echo "<td> $day_num </td>";
$day_num++;
$day_count++;
I have a second table that calls a separate query for each day of the month:
<tr>
<td> </td>
<td> </td>
<td><?php do { ?>
<?php echo $row_apr1['appt_time']; ?> <em><?php echo $row_apr1['nickname']; ?></em><br>
<?php } while ($row_apr1 = mysql_fetch_assoc($apr1)); ?></td>
<td><?php do { ?>
<?php echo $row_apr2['appt_time']; ?> <em><?php echo $row_apr2['nickname']; ?></em><br>
<?php } while ($row_apr2 = mysql_fetch_assoc($apr2)); ?></td>
<td><?php do { ?>
<?php echo $row_apr3['appt_time']; ?> <em><?php echo $row_apr3['nickname']; ?></em><br>
<?php } while ($row_apr3 = mysql_fetch_assoc($apr3)); ?></td>
<td><?php do { ?>
<?php echo $row_apr4['appt_time']; ?> <em><?php echo $row_apr4['nickname']; ?></em><br>
<?php } while ($row_apr4 = mysql_fetch_assoc($apr4)); ?></td>
<td><?php do { ?>
<?php echo $row_apr5['appt_time']; ?> <em><?php echo $row_apr5['nickname']; ?></em><br>
<?php } while ($row_apr5 = mysql_fetch_assoc($apr5)); ?></td>
</tr>
Is there a way I can merge the code together into one table, like using the $day_num value to call the query code?
Example: April 4th - $day_num would be = 4 and display a 4 in the table. But then can't I use php concatenation or something to call the code:
<br><?php echo $row_apr4['appt_time']; ?> <em><?php echo $row_apr4['nickname']; ?></em><br>
<?php } while ($row_apr4 = mysql_fetch_assoc($apr4)); ?>
Sorry if this has been done before a million times. I've been searching and can't seem to find what I'm looking for.
Thanks!
You can use the variable variables or $$ option. http://www.php.net/manual/en/language.variables.variable.php
<td>
<?php
$variable_name = 'apr' . $day_num;
do { ?>
<?php echo $row['appt_time']; ?>
<em><?php echo $row['nickname']; ?></em><br>
<?php } while ($row = mysql_fetch_assoc($$variable_name)); ?>
</td>
Not sure this is the best option for you but based off the code you have supplied it is a possible solution.
FYI you shouldn't use mysql_ functions as they are deprecated.