Dynamic Accordion Repetitive Code - php

I have built a navigation page in an accordion format using mysqli and php. You can see an example at
[1]: http://www.outerhebridesalgae.uk/marine/marine-taxononic-list.php/ "Marine Algae".
This works perfectly well, but the coding is very repetitive as I am repeating the same code for each heading in the accordion. This is fine for a limited number of headings, but some of my tables have 30 or more headings! Is there a more efficient approach? Thank you for your assistance.
A sample of the code follows:
<?phprequire '../assets/includes/conn.inc.php';
$query1 = mysqli_query($connect, "SELECT * FROM marine WHERE famid='1' ORDER BY marine.species");
$row1 = mysqli_fetch_assoc($query1);
$totalRows_query1 = mysqli_num_rows($query1);
$query2 = mysqli_query($connect, "SELECT * FROM marine WHERE famid='2' ORDER BY marine.species");
$row2= mysqli_fetch_assoc($query2);
$totalRows_query2 = mysqli_num_rows($query2);
$query4 = mysqli_query($connect, "SELECT * FROM marine WHERE famid='4' ORDER BY marine.species");
$row4= mysqli_fetch_assoc($query4);
$totalRows_query4 = mysqli_num_rows($query4);
?>
<!DOCTYPE html>
<head>.......</head>
<body>
<div class="uk-panel uk-panel-box uk-margin">
<h2 class="uk-panel-title">Chlorophyta - Green Seaweeds</h2>
<div id="firstpane" class="msg_list">
<p class="msg_head"> <?php echo $row1['family']; ?><img src="../images/arrow.jpg" alt="arrow open" align="right"></p>
<div class="msg_body">
<table width="100%" >
<?php do { ?>
<tr>
<td id="marine.species"><?php echo $row1['species']; ?></td>
<?php } while ($row1 = mysqli_fetch_assoc($query1)); ?>
</tr>
</table>
</div>
<p class="msg_head"><?php echo $row2['family']; ?><img src="../images/arrow.jpg" alt="arrow open" align="right"></p>
<div class="msg_body">
<table width="100%" >
<?php do { ?>
<tr>
<td id="marine.species"><?php echo $row2['species']; ?></td>
<?php } while ($row2 = mysqli_fetch_assoc($query2)); ?>
</tr>
</table>
</div>
<p class="msg_head"><?php echo $row4['family']; ?><img src="../images/arrow.jpg" alt="arrow open" align="right"></p>
<div class="msg_body">
<table width="100%" >
<?php do { ?>
<tr>
<td id="marine.species"><?php echo $row4['species']; ?></td>
<?php } while ($row4 = mysqli_fetch_assoc($query4)); ?>
</tr>
</table>
</div></body></html>

Related

PHP if else question regarding simple to do list

To do list:
I have created the HTML and php code to insert data into the database, and read from it.
Database is looking like this:
task_id |task_desc |task_target |task_finished_date |task_status
40 |test p |2020-07-25 |2020-07-23 |completed
Task_target and task_finished_date are using type date in the database.
task_target is getting value on creation of the task, and task_finished_date is getting automatically value when we mark the job completed using now():
$task_id = $_GET['completed'];
mysqli_query($db, "UPDATE tasks SET task_status = 'completed' WHERE task_id=". $task_id);
mysqli_query($db, "UPDATE tasks SET task_finished_date = now() WHERE task_id=". $task_id);
mysqli_query($db, "ORDER BY task_finished_date DESC ". $task_id);
header("Location: index.php" );
}
I am currently trying to program it when some task is completed AFTER the deadline to be displayed in "Failed" table, and if is closed before the deadline to be displayed in completed table.
<tbody>
<?php $i = 1; while ($row = mysqli_fetch_array($finished_tasks)) { ?>
<tr>
<?php
if($targetd > $finished_d){ ?>
<td class="successful"> <?php echo $i; ?> </td>
<td class="successful"> <?php echo $row['task_desc']; ?> </td>
<td class="successful"> <?php echo $row['task_finished_date']; ?> </td>
<?php }?>
</tr>
<?php $i++; } ?>
</tbody>
<thead>
<tr>
<th>N</th>
<th>Failed Tasks</th>
<th style="width: 197px;">Task finished time</th>
</tr>
</thead>
<tbody>
<?php $i = 1; while ($row = mysqli_fetch_array($finished_tasks)) { ?>
<tr>
<?php
if($targetd < $finished_d){ ?>
<td class="failed"> <?php echo $i; ?> </td>
<td class="failed"> <?php echo $row['task_desc']; ?> </td>
<td class="failed"> <?php echo $row['task_finished_date']; ?> </td>
<?php }?>
</tr>
<?php $i++; } ?>
</tbody>
As in the $finished_tasks variable i have storing the values for the completed tasks:
$finished_tasks = mysqli_query($db, "SELECT * FROM tasks WHERE task_status = 'completed' ");
$finished_task_time = mysqli_query($db, "SELECT task_finished_date FROM tasks WHERE task_status = 'completed' ");
$planned_target_time = mysqli_query($db, "SELECT task_target FROM tasks WHERE task_status = 'completed' ");
while ($row = mysqli_fetch_array($finished_tasks)) {
$targetd = $row['task_target'];
$finished_d = $row['task_finished_date'];
Have tried to get the deadline(task_target)and target(task_finished_date) and insert them in both variables and then compare them, and based on this comparison to display them where I want, but the logic here is not right I think.
How I can separate the tasks based on the desired condition?
Entire Code Below:
<?php
// initialize errors variable
$errors = "";
// connect to database
$db = mysqli_connect("localhost", "root", "", "todo");
// insert a quote if submit button is clicked
if (isset($_POST['submit'])) {
if (empty($_POST['task_desc'])) {
$errors = "You must fill in the task";
}else{
$task_desc = $_POST['task_desc'];
$task_target = $_POST['task_target'];
$sql = "INSERT INTO tasks (task_desc, task_target) VALUES ('$task_desc', '$task_target')";
mysqli_query($db, $sql);
header('location: index.php');
}
}
// delete task
if (isset($_GET['del_task'])) {
$task_id = $_GET['del_task'];
mysqli_query($db, "DELETE FROM tasks WHERE task_id=".$task_id);
header('location: index.php');
}
if(isset($_GET['completed'])){
$task_id = $_GET['completed'];
mysqli_query($db, "UPDATE tasks SET task_status = 'completed' WHERE task_id=". $task_id);
mysqli_query($db, "UPDATE tasks SET task_finished_date = now() WHERE task_id=". $task_id);
mysqli_query($db, "ORDER BY task_finished_date DESC ". $task_id);
header("Location: index.php" );
}
// select all tasks if page is visited or refreshed
$tasks = mysqli_query($db, "SELECT * FROM tasks WHERE task_status = 'Active' ");
?>
<!DOCTYPE html>
<html>
<head>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bootstrap Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js"></script>
<link rel="stylesheet" type="text/css" href="style.css">
<title>ToDo List Application PHP and MySQL</title>
</head>
<body>
<!-- Page Content -->
<div class="container">
<div class="row">
<!-- Blog Entries Column -->
<div class="col-md-12">
<div class="heading">
<h2 style="font-style: 'Hervetica';">ToDo List Application PHP and MySQL database</h2>
</div>
<form method="post" action="index.php" class="input_form">
<?php if (isset($errors)) { ?>
<p><?php echo $errors; ?></p>
<?php } ?>
<label for="task_target">Select Date:</label>
<input type="date" class="form-control" name="task_target">
<label for="task_desc">Describe your task:</label>
<textarea class="form-control" name="task_desc" id="body" cols="30" rows="5"></textarea>
<button type="submit" name="submit" id="add_btn" class="form-control">Add Task</button>
</form>
<table class="table table-bordered">
<thead>
<tr>
<th>N</th>
<th>Ongoing Tasks</th>
<th>Target Date</th>
<th>Action</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php $i = 1; while ($row = mysqli_fetch_array($tasks)) { ?>
<tr>
<td> <?php echo $i; ?> </td>
<td> <?php echo $row['task_desc']; ?> </td>
<td> <?php echo $row['task_target']; ?> </td>
<td>
Delete
</td>
<td>
Complete
</td>
</tr>
<?php $i++; } ?>
</tbody>
</table>
<?php
$finished_tasks = mysqli_query($db, "SELECT * FROM tasks WHERE task_status = 'completed' ");
$finished_task_time = mysqli_query($db, "SELECT task_finished_date FROM tasks WHERE task_status = 'completed' ");
$planned_target_time = mysqli_query($db, "SELECT task_target FROM tasks WHERE task_status = 'completed' ");
while ($row = mysqli_fetch_array($finished_tasks)) {
$targetd = $row['task_target'];
$finished_d = $row['task_finished_date'];
?>
<table class="table table-bordered">
<thead>
<tr>
<th>N</th>
<th>Successful Tasks</th>
<th style="width: 197px;">Task finished time</th>
</tr>
</thead>
<tbody>
<?php $i = 1; while ($row = mysqli_fetch_array($finished_tasks)) { ?>
<tr>
<?php
if($targetd > $finished_d){ ?>
<td class="successful"> <?php echo $i; ?> </td>
<td class="successful"> <?php echo $row['task_desc']; ?> </td>
<td class="successful"> <?php echo $row['task_finished_date']; ?> </td>
<?php }?>
</tr>
<?php $i++; } ?>
</tbody>
</table>
<table class="table table-bordered">
<thead>
<tr>
<th>N</th>
<th>Failed Tasks</th>
<th style="width: 197px;">Task finished time</th>
</tr>
</thead>
<tbody>
<?php $i = 1; while ($row = mysqli_fetch_array($finished_tasks)) { ?>
<tr>
<?php
if($targetd > $finished_d){ ?>
<td class="failed"> <?php echo $i; ?> </td>
<td class="failed"> <?php echo $row['task_desc']; ?> </td>
<td class="failed"> <?php echo $row['task_finished_date']; ?> </td>
<?php }?>
</tr>
<?php $i++; } ?>
</tbody>
</table>
<?php } ?>
</div>
</div>
</div>
</body>
</html>
Your question is quite ambiguous, what kind of error or unexpected output are you getting?
Had to strip your code of the HTML part and the PHP part not affecting the logic, here's what I have.
$finished_tasks = mysqli_query($db, "SELECT * FROM tasks WHERE task_status = 'completed' ");
$finished_task_time = mysqli_query($db, "SELECT task_finished_date FROM tasks WHERE task_status = 'completed' ");
$planned_target_time = mysqli_query($db, "SELECT task_target FROM tasks WHERE task_status = 'completed' ");
while ($row = mysqli_fetch_array($finished_tasks)) {
$targetd = $row['task_target'];
$finished_d = $row['task_finished_date'];
$i = 1;
while ($row = mysqli_fetch_array($finished_tasks))
{
if($targetd > $finished_d)
{
echo $i;
echo $row['task_desc'];
echo $row['task_finished_date'];
$i++;
}
$i = 1;
while ($row = mysqli_fetch_array($finished_tasks))
{
if($targetd > $finished_d)
{
echo $i;
echo $row['task_desc'];
echo $row['task_finished_date'];
}
$i++;
}
}
All three while loops assign a value to the $row variable. This would cause some unexpected results as some values may be skipped.
You're referencing the SQL result indexes inappropriately.
I have managed to find a easy fix of my question.
First of all thanks to all who tried to help me and advised way of effective learning.
Really like this place.
So, on the answer:
As in the begging i have described that the Deadline of the task is defining during the creation of the task, and the closing time is getting to the database when the user click Completed button.
So i have decided to have the sort directly into the database modifying the queries like this:
$finished_tasks = mysqli_query($db, "SELECT * FROM tasks WHERE task_status = 'completed' AND task_finished_date < task_target ");
$finished_failed_tasks = mysqli_query($db, "SELECT * FROM tasks WHERE task_status = 'completed' AND task_finished_date > task_target ");
In this way i have all of the completed successfully tasks in $finished_tasks variable and all of the failed in $finished_failed_tasks, after that the displaying in the page is easy.
I am not quite sure if this is practical solution, but in this way working as i desired.
Next step is to strip and refactor the code as you suggested.
Once again, thanks really much and have a nice day.
Regards, Kristiyan

Custom Pagination Not Working Well in Wordpress

Im just New in Wordpress and in PHP .. Now Ive just uploaded a theme in Wordpress. The Problem is The Custom Pagination is not working in Wordpress but works well in USBWebserver. It Only Just show the Same Content
<table id="maincon2-Article">
<?php
$page2 = $_GET["page2"];
if($page2 == "" || $page2 == "1"){
$pages = 0;
}
else{
$pages = ($page2*3)-3;
}
$Mobartsql = "Select * from tbl_article ORDER BY `Article_ID` DESC limit 1,3";
$result4 = $con->query($Mobartsql);
while($row4 = $result4->fetch_assoc()){
echo '<tr>
<td style="width:50%">
<img src = "https://www.stlouisreviewcenter.com/wp-content/uploads/slrc/'.$row4["Art_pic"].'" id="artpiclink2"/>
</td>
<td style="padding-left:2%" >
<h1 id="art-head2" style="color:purple">'.$row4["Art_title"].' </h5>
</td>
</tr>';
$results1 = $con->query("Select * from tbl_article ORDER BY `Article_ID` DESC");
$rows2 = $result1->num_rows/3;
$rows2 = ceil($rows2);
}
echo '<tr>
<td><br></td>
</tr>
<tr>
<td colspan="2">';
for($i=1; $i<=$rows;$i++){
?> <button> <a href="/article/?page=<?php echo $i;?>" style="text-decoration:none;"/><?php echo $i; ?></a> </button>
<?php
}
?>
</td>
</tr>
<tr>
<td><br/></td>
</tr>
</table>
Here is the Sample .. Plz Need Help ..

have two users side by side in a members page

so I would like in my registered users page, or members page all my users on it (that's the easy part) where i'm having trouble is I would like to have 2 users side by side, and then on the next line 2 other users and so on.
Something like this:
enter image description here
i'm using php and it's to use after a php query from mysql, this is my code at the moment:
<?php
include '_database/database.php';
session_start();
$current_user = $_SESSION['user_username'];
$sql = "SELECT * FROM users order by user_id desc";
$result = mysqli_query($database,$sql) or die(mysqli_error($database));
while($rws = mysqli_fetch_array($result)){
?>
<table style="width: 200px;">
<tbody>
<tr>
<td style="width: 100px;"><img src="userfiles/avatars/<?php echo $rws['user_avatar'];?>" width="100" height="100"></td>
<td style="width: 200px;">
<?php echo $rws['user_country'];?> <?php echo $rws['user_username'];?><br>
<?php echo $rws['user_joindate'];?><br>
1<br>
2<br>
3<br>
See more
</td>
</tr>
</tbody>
</table>
<?php } ?>
Thank you !
<?php
include '_database/database.php';
session_start();
$current_user = $_SESSION['user_username'];
$sql = "SELECT * FROM users order by user_id desc";
$result = mysqli_query($database,$sql) or die(mysqli_error($database));
?>
<div class="container" style="width: 200px;">
<?php while($rw = mysqli_fetch_array($result)){ ?>
<div class="user" style="width: 100px; float: left;">
<img src="userfiles/avatars/<?php echo $rw['user_avatar'];?>" width="100" height="100">
<div class="userInfo">
<?php echo $rw['user_country'] . ' ' . $rw['user_username'];?><br>
<?php echo $rw['user_joindate'];?><br>
<!--Content--><br>
<!--Content--><br>
<!--Content--><br>
See more
</div>
</div>
<?php } ?>
</div>
i haven't tested the code.
You also can use for design HTML, CSS and JS frameworks like Bootstrap

Showing data from category with php mysql

My tables are:
barangtbl: id, judul_barang, judul_seo, keywords, deskripsi, id_kat, id_sub, id_supersub, kategori_seo, view, gambar
kategori: id_kat, nama_kat
subkategori: id_sub, id_kat, nama_sub
supersubkategori: id_supersub, id_sub, id_kat, nama_supersub
I have a problem with showing data in category from database with PHP, the problem is when i click link: localhost/test/category.php?name=HPI, it doesn't show any data, but if I change HPI with number: 15, it show all.
15 is id_supersub data on supersubkategori table where I join with barangtbl table. So, all i want is if someone click: localhost/test/category.php?name=HPI it will show data with HPI category inside. How solve this problem?
<?php
if (isset($_GET['name']))
{
$kategori = $_GET['name'];
}
include "config.php";
if ((isset($kategori)) =='')
{
$query = "SELECT * FROM barangtbl INNER JOIN supersubkategori on supersubkategori.id_supersub = barangtbl.id_supersub ORDER BY id DESC LIMIT 0,12";
$hasil = mysql_query($query);
$numrows = mysql_num_rows($hasil);
}
else
{
echo "
<table width=\"100%\">
<tr>
<td align=\"center\"><b><font color=\"red\" size=\"2.5\">[ ".$_GET['name']." ]</b></font></td>
</tr>
</table>";
$query = "SELECT * FROM barangtbl WHERE id_supersub = '$kategori' ORDER BY id";
$hasil = mysql_query($query);
$numrows = mysql_num_rows($hasil);
}
?>
<table cellpadding="10" cellspacing="2" align="center">
<tr>
<?php
$kolom=3;
$x = 0;
if($numrows > 0)
{
while($data=mysql_fetch_array($hasil))
{
if ($x >= $kolom)
{
echo "</tr><tr>";
$x = 0;
}
$x++;
?>
<th>
<div id="title">
<a href="product.php?id=<?php echo $data['id']; ?>">
<?php echo $data['judul_barang']; ?>
</a>
<br><br>
</div>
<div id="image">
<a href="product.php?id=<?php echo $data['id']; ?>">
<img width='150' height='150' valign='top' border='1,5' src="product/<?php echo $data['gambar']; ?>" />
</a>
<br><br>
</div>
<div id="action">
<?php
echo '
<a href="product.php?id='.$data['id'].'">
<img src="images/detail.jpg"\ title="Detail Barang" border="0" width=\"50\" height=\"30\">
</a>';
?>
</div>
<hr />
</th>
<?php
}
}
?>
</tr>
</table>
Try removing the quotes
$query = "SELECT * FROM barangtbl WHERE id_supersub = $kategori ORDER BY id";

Session not working as it has to be, auto session lost or content loss upon refresh page

I am new is PHP, doing my final year project in Student Result Online System.
In my system, I have an error in session, Once the user/student logs in using their user name and password, the dashboard displays his details properly.
But, as soon as the page reloads or you click to view other contents or just simply click on the student's tool bar within dashboard, the page loses all of its relevant contents and becomes idle or blank.
I want to prevent auto session destroy or loss while the user remains on the same page until he/she logs-out. any help would be grateful. Interesting thing is that, this codes works really well in Localhost (Wamp Server), but it loses its session in Online (cpanel).
My codes pages are:
login.php
<?php
include('dbcon.php');
if (isset($_POST['login'])){
session_start();
$student_no = $_POST['student_no'];
$password = $_POST['password'];
$query = "SELECT * FROM students WHERE student_no='$student_no' AND password='$password' and status = 'active' ";
$result = mysql_query($query)or die(mysql_error());
$num_row = mysql_num_rows($result);
$row=mysql_fetch_array($result);
if( $num_row > 0 ) {
header('location:dasboard.php');
$_SESSION['id']=$row['student_id'];
}
elae{
header('location:access_denied.php');
}
}
?>
session.php
<?php
session_start();
if (!isset($_SESSION['id']) || (trim($_SESSION['id']) == '')) {
header("location: index.php");
exit();
}
$session_id=$_SESSION['id'];
?>
dasboard.php
<?php include('session.php'); ?>
<?php include('header.php'); ?>
<?php include('navbar.php'); ?>
<?php
$query=mysql_query("select * from students where student_id='$session_id'")or die(mysql_error());
$row=mysql_fetch_array($query);
$year_level = $row['year_level'];
$term = $row['term'];
$status = $row['student_status'];
$school_year = $row['year_level'];?>
<div class="container">
<div class="margin-top">
<div class="row">
<?php include('head.php'); ?>
<div class="span12">
<div class="grade">
<?php include('grade_option.php'); ?>
</div>
</div>
<div class="span2">
<?php include('user_sidebar.php'); ?>
</div>
<div class="span10">
<table cellpadding="0" cellspacing="0" border="0" class="table table-bordered" id="example">
<thead>
<tr>
<th width="100">Code</th>
<th width="300">Subject</th>
<th width="50">Units</th>
<th>Gen Ave.</th>
<th>Term</th>
<th>Year Level</th>
<th>Remarks</th>
</tr>
</thead>
<tbody>
<?php $user_query=mysql_query("select * from grade where student_id = '$session_id' and school_year = '$year_level'
and semester = '$term'
")or die(mysql_error());
while($row=mysql_fetch_array($user_query)){
$id=$row['grade_id'];
$remarks = $row['remarks'];
$subject_id = $row['subject_id'];
$subject_query = mysql_query("select * from subject where subject_id = '$subject_id'")or die(mysql_error());
while($subject_row=mysql_fetch_array($subject_query)){
?>
<tr>
<td>
<?php echo $subject_row['code']; ?></td>
<td><?php echo $subject_row['title']; ?></td>
<td><?php echo $subject_row['unit']; ?></td>
<td><?php echo $row['gen_ave']; ?></td>
<td><?php echo $row['semester']; ?></td>
<td><?php echo $row['school_year']; ?></td>
<?php if ($remarks == 'Very Good'){ ?>
<td><span class="very_good"><?php echo $row['remarks']; ?></span></td>
<?php }else if($remarks == 'Excellent'){ ?>
<td><span class="Excellent"><?php echo $row['remarks']; ?></span></td>
<?php }else if($remarks == 'Satisfactory'){ ?>
<td><span class="sat"><?php echo $row['remarks']; ?></span></td>
<?php }else if($remarks == 'Fair'){ ?>
<td><span class="fair"><?php echo $row['remarks']; ?></span></td>
<?php }else if($remarks == 'Failed'){ ?>
<td><span class="failed"><?php echo $row['remarks']; ?></span></td>
<?php }else if($remarks == 'Incomplete'){ ?>
<td><span class="failed"><?php echo $row['remarks']; ?></span></td>
<?php }else if($remarks == 'Officially Dropped'){ ?>
<td><span class="drop"><?php echo $row['remarks']; ?></span></td>
<?php }else if($remarks == 'PASS'){ ?>
<td><span class="Excellent"><?php echo $row['remarks']; ?></span></td>
<?php }else if ($remarks == ''){ ?>
<td><?php echo $row['remarks']; ?></td>
<?php } ?>
</tr>
<?php }} ?>
</tbody>
</table>
</div>
<?php include('units_table.php'); ?>
<?php include('gwa_table.php'); ?>
<?php include('cwa_table.php'); ?>
</div>
<?php include('grading_system.php') ?>
</div>
</div>
</div>
<?php include('footer.php') ?>
In your following code:
if( $num_row > 0 ) {
header('location:dasboard.php');
$_SESSION['id']=$row['student_id'];
}
You do
header('location:dasboard.php');
Before you set the session:
$_SESSION['id']=$row['student_id'];
Meaning you redirect first, and session is NEVER set.
By the way header.php, includes header.php?
And
elae{
header('location:access_denied.php');
}
Should be else of course.

Categories