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.
Related
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
}
?
Why are values are getting printed multiple times (see images below). I need to print them only once.
<?php foreach($patchData3 as $tes3){?>
<?php foreach($patchData1 as $tes){?>
<tr class="<?php if($tes->PATCH == $tes3->PATCH) {echo "red_color"; } ?>">
<td><?php echo $tes->HOSTNAME;?></td>
<td><?php echo $tes->VERSION;?></td>
<td><?php echo $tes->PATCH;?></td> <!--bgcolor="#FF0000"-->
</tr>
<?php } ?>
<?php }?>
<?php foreach($patchData1 as $tes){ ?>
<tr class="<?php if(checkfunction($tes->PATCH,$patchData3) == TRUE) { echo "red_color"; } ?>">
<td><?php echo $tes->HOSTNAME;?></td>
<td><?php echo $tes->VERSION;?></td>
<td><?php echo $tes->PATCH;?></td> <!--bgcolor="#FF0000"-->
</tr>
<?php } ?>
<?php
function checkfunction($patch,$patchData3){
foreach($patchData3 as $tes3){
if($patch == $tes3->PATCH){
return true;
}
}
}
?>
I used a function to overcome the duplication. Please comment if it does not work.
Controller
public function admin()
{
// get the current date employee logged in details
$data['get_attendance'] = $this->attendance_model->get_attendance();
// get the active employee details
$data['get_employee'] = $this->attendance_model->get_employee();
//print_r($data['get_employee']); exit();
// attendance page
$data['header'] = "Employee";
$data['sub_header'] = "Attendance employee";
$data['main_content'] = 'attendance/admin_list';
$this->load->view('employeelayout/main',$data);
}
Model
public function get_attendance()
{
return $this->db->where('date',date('Y-m-d'))->get('attendance')->result_array();
}
public function get_employee()
{
return $this->db->where('is_active',1)->get('employee')->result_array();
}
Views
<?php $count = 1 ?>
<?php foreach ($get_employee as $employee) { ?>
<tr class="gradeX">
<td><?php echo $count++ ?></td>
<td><?php echo $employee['first_name'] . ' ' . $employee['last_name'] ?></td>
<td><?php echo $employee['employee_number'] ?></td>
<?php foreach ($get_attendance as $attendance) : ?>
<!-- if employee exists -->
<?php if($employee['employee_id'] == $attendance['employee_id'] ) { ?>
<td><?php echo date('M-Dj-Y',strtotime($attendance['date'])) ?></td>
<td><?php echo $attendance['mark_in_time'] ?></td>
<td>mark out going</td>
<td>Active</td>
<?php break; ?>
<?php } elseif($employee['employee_id'] != $attendance['employee_id'] ) { ?>
<td><?php echo "i'm absent" ?></td>
<?php break; ?>
<?php } ?>
<?php endforeach; ?>
</tr>
<?php } ?>
I want to display the currently logged in (present) employee and display the absent employees too. I'm getting the employee details from employee table.
and attendance details from attendance table. if an employee is absent and I want to display the absent else display the login details. where here foreach loop not displaying properly with if condition. what's wrong with the loop.
try this:
<?php foreach ($get_employee as $employee) : ?>
<tr class="gradeX">
<td><?php echo $count++ ?></td>
<td><?php echo $employee['first_name'] . ' ' . $employee['last_name'] ?></td>
<td><?php echo $employee['employee_number'] ?></td>
<?php foreach ($get_attendance as $attendance) : ?>
<!-- if employee exists -->
<?php if($employee['employee_id'] == $attendance['employee_id']) : ?>
<td><?php echo date('M-Dj-Y',strtotime($attendance['date'])) ?></td>
<td><?php echo $attendance['mark_in_time'] ?></td>
<td>mark out going</td>
<td>Active</td>
<?php elseif ($employee['employee_id'] != $attendance['employee_id']) : ?>
<td><?php echo "i'm absent" ?></td>
<?php else : ?>
<?php endif; ?>
<?php endforeach; ?>
</tr>
<?php endforeach; ?>
Apply left join in place of two different queries.
Because currently you have applied two loops and their comparisons, will consume a lot of execution time And I don't think this is the good code If you get thousands of data than definitely you can analyse the execution time.
So it's better to apply join query.
I want to display based on the year with repeated first_ ids and actual ids. but actual_id is unique.
<?php while($Row=oci_fetch_assoc($Result)){
$ResultArray[$j]['First_ID']=$Row['First_ID'];
$ResultArray[$j]['Actual_ID']=$Row['Actual_ID'];
$ResultArray[$j]['Title']=$Row['Title'];
$ResultArray[$j]['START_DATE']=$Row['START_DATE'];
$startDate=explode(' ',$Row['START_DATE']);
$ResultArray[$j]['YEAR']=$startDate[0];
if(($startDate[0])==($startyear)){
$ResultArray[$j][$startyear]['Actual_ID']=$Row['Actual_ID'];
$ResultArray[$j][$startyear]['Other_ID']=$Row['Other_ID'];
$ResultArray[$j][$startyear]['Content']=$Row['Content'];
}
if(($startDate[0])==($startyear+1)){
$ResultArray[$j][$startyear+$i]['Actual_ID']=$Row['Actual_ID'];
$ResultArray[$j][$startyear+$i]['Other_ID']=$Row['Other_ID'];
$ResultArray[$j][$startyear+$i]['Content']=$Row['Content'];
}
if(($startDate[0])==($startyear+1)){
$ResultArray[$j][$startyear+$i]['Actual_ID']=$Row['Actual_ID'];
$ResultArray[$j][$startyear+$i]['Other_ID']=$Row['Other_ID'];
$ResultArray[$j][$startyear+$i]['Content']=$Row['Content'];
}
$j++;
}?>
//to display in table
it display year with asc order first and then next year etc.
<table class="tab">
<tr><th>S.No</th>
<th>first_ id</th><th>Title</th>
<?phpfor($i=0;$i<4;$i++){?><th colspan="3"><?php echo $startyear+$i; ?> </th>
<?php }?>
<tr><th></th><th></th><th></th>
<th>Actual_ID</th><th>Other_ID</th><th>Content</th>
<th>Actual_ID</th><th>Other_ID</th> <th>Content</th>
<th>Actual_ID</th><th>Other_ID</th><th>Content</th>
<th>Actual_ID</th><th>Other_ID</th><th>Content</th></tr>
<?php
foreach($ResultArray as $ResultArray1){
//print_r($abstarctResultArray1);
?><tr><td><?php echo $count; $count++;?></td>
<td><?php echo $ResultArray1['First_ID'];?></td>
<td><?php echo $ResultArray1['Title'];?></td>
<?php for($i=0;$i<4;$i++){
$startDate=explode(' ',$ResultArray1['START_DATE']);
if(($startDate[0]==$startyear)){?>
<td><?php echo $ResultArray1[$startDate[0]['Actual_ID'];?></td>
<td><?php echo $ResultArray1[$startDate[0]['Other_ID'];?></td>
<td><?php echo $ResultArray1[$startDate[0]['Content'];?></td>
<?php }
else if(($startDate[0]==$startyear+1)){?>
<td><?php echo $ResultArray1[$startDate[0]['Actual_ID'];?></td>
<td><?php echo $ResultArray1[$startDate[0]['Other_ID'];?></td>
<td><?php echo $ResultArray1[$startDate[0]['Content'];?></td>
</tr>
<?php
}
else if(($startDate[0]==$startyear+2)){?>
<td><?php echo $ResultArray1[$startDate[0]['Actual_ID'];?></td>
<td><?php echo $ResultArray1[$startDate[0]['Other_ID'];?></td>
<td><?php echo $ResultArray1[$startDate[0]['Content'];?></td>
</tr>
<?php
}
}
}
?>
I have tried with different ideas, but I can't. Please help.
I want to prevent amaun_caj , amaun_pelbagai , amaun_penalti , amaun_tunggakan from being repeated so if the id_akaun is the same the data will not appear, my code only works on amaun_caj, and for the rest, not a single data appear, what's the problem?
<?php
$i = 0; $id_akaun_old ="";
while($output = mysql_fetch_array($result)) {
$i++;
$id_akaun = $output["id_akaun"];
$lokasi = $output["lokasi"];
$amaun_caj = $output["amaun_caj"];
$amaun_tunggakan = $output["amaun_tunggakan"];
$amaun_penalti = $output["amaun_penalti"];
$amaun_pelbagai = $output["amaun_pelbagai"];
$jumlah_bayaran = $output["jumlah_bayaran"];
?>
<tr>
<td>
<?php echo $i; ?>
</td>
<td>
<?php echo $jenis; ?>
</td>
<td>
<?php echo $id_akaun;
?>
</td>
<td>
<?php echo $no_telefon; ?>
</td>
<td>
<?php echo $lokasi; ?>
</td>
<td align="center">
<?php
if($id_akaun != $id_akaun_old):
echo $amaun_caj;
$id_akaun_old = $id_akaun;
else: echo '';
endif;?>
</td>
<td align="center">
<?php
if($id_akaun != $id_akaun_old):
echo $amaun_pelbagai;
$id_akaun_old = $id_akaun;
else: echo '';
endif;?>
</td>
<td align="center">
<?php
if($id_akaun != $id_akaun_old):
echo $amaun_penalti;
$id_akaun_old = $id_akaun;
else: echo '';
endif;?>
</td>
<td align="center">
<?php
if($id_akaun != $id_akaun_old):
echo $amaun_tunggakan;
$id_akaun_old = $id_akaun;
else: echo '';
endif;?>
</td>
<td align="center">
<?php
if($id_akaun != $id_akaun_old):
echo $jumlah_bayaran;
$id_akaun_old = $id_akaun;
else: echo '';
endif;?>
</td>
<?php
}
?>
Using temporary variable $old_id_akaun might not be the best practice. If you still want to do it like that, i suggest you user ORDER BY id_akaun in your SQL syntax.
In my oppinion, you might get rid of temporary variable and follow these steps,
1. Create empty array outside your while loop. For the sake of easy
understanding, let's called it $list_id_akaun.
2. Inside your while loop, after you get $id_akaun, check whether $id_akaun
is inside $list_id_akaun
3. If not exists, insert it to $list_id_akaun and continue echoing your
table row
4. If exists, skip to the next row.
I don't know why you don't want to use a select distinct clause in this case, but just put them inside a container then check inside the loop:
<?php
$temp = array();
$i = 1;
while($output = mysql_fetch_array($result)):
$id_akaun = $output["id_akaun"];
$lokasi = $output["lokasi"];
$amaun_caj = $output["amaun_caj"];
$amaun_tunggakan = $output["amaun_tunggakan"];
$amaun_penalti = $output["amaun_penalti"];
$amaun_pelbagai = $output["amaun_pelbagai"];
$jumlah_bayaran = $output["jumlah_bayaran"];
?>
<tr>
<td><?php echo $i; $i++; ?></td>
<td><?php echo $jenis; ?></td>
<td><?php echo $id_akaun; ?></td>
<td><?php echo $no_telefon; ?></td>
<td><?php echo $lokasi; ?></td>
<?php if(!in_array($id_akaun, $temp)): ?>
<td><?php echo $amaun_penalti; ?></td>
<td><?php echo $amaun_tunggakan; ?></td>
<td><?php echo $jumlah_bayaran; ?></td>
<?php $temp[] = $id_akaun; ?>
<?php else : ?>
<td></td><td></td><td></td>
<?php endif; ?>
</tr>
<?php endhile; ?>