Make dropdown sort list - php

I have this php code with a drop down pulling info from the database but cannot seem to figure out how to get the drop down selection to sort the list...
The drop down is showing the course name as the label but when selected needs to sort the list by course id.
Visit Here to see the table in action
<?php
$connect = mysql_connect('localhost','emscompl_paramed','PASSOWRD) or die(mysql_error());
$selectdb = mysql_select_db('emscompl_joom1283',$connect);
$sel = "SELECT us.fullname, s . *
FROM registered_users AS `us`
LEFT OUTER JOIN course_students AS s ON s.userid = us.userid";
$ressel = mysql_query($sel);
$fetchsel = mysql_fetch_array($ressel);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<style type="text/css">
.titiel {
font-weight: bold;
}
td {
border-right-width: thin;
border-bottom-width: thin;
border-right-style: dotted;
border-bottom-style: solid;
text-align:center;
}
th {
background-color:#000000;
color: #FFF;
}
tr:nth-child(odd) { background-color:#eee; }
tr:nth-child(even) { background-color:#fff; }
</style>
</head>
<body>
<p class="titiel">Pre-Entrance Document Report</p>
<p> Please Select Course for Report</p>
<form method="post" action="preentrancereportsorted.php">
<label for="select"><select name="course" value="Select" size="1">
<?php
$sql = "SELECT * FROM courses";
$result = mysql_query($sql) or die (mysql_error());
while ($row = mysql_fetch_array($result))
{
$id=$row["id"];
$course=$row["coursename"];
$options.="<OPTION VALUE=\"$id\">".$course;
}
?>
<option>
<? echo $options ?>
</option>
</select>
<input type="submit" name="Submit" value="Generate Report">
</form>
<table width="1246" height="56">
<tr>
<th width="147" height="50">Student Name</td>
<th width="15">R</th>
<th width="18">M</th>
<th width="18">L</th>
<th width="81">Background</th>
<th width="83">Drug Screen</th>
<th width="112">Clear Background</th>
<th width="113">Clean Drug Screen</th>
<th width="97">Student Info</th>
<th width="88">School App</th>
<th width="117">Professional Recomendation</th>
<th width="119">Reasonable Accomadations</th>
<th width="59">Drivers Licesnse</th>
<th width="91">High School Diploma</th>
</tr>
<?php while ($row = mysql_fetch_array($ressel)) { ?>
<td width="146" height="50"><?php echo $row['fullname'];?></td>
<td width="17"><?php echo $row['entrancereadingscore'];?></td>
<td width="17"><?php echo $row['mathscore'];?></th>
<td width="17"><?php echo $row['locatinginfoscore'];?></td>
<td width="84"> <?php echo $row['backcalc'];?></td>
<td width="79"><?php echo $row['drugcalc'];?></td>
<td width="113"><?php if ($row['clearbackground']='1')
{
echo "Yes";
}
else
{
echo "no";
}
?></td>
<td width="114">
<?php if ($row['cleardrugtest']=='1')
{
echo "Yes";
}
else
{
echo "No";
}
?>
</td>
<td width="96">
<?php if ($row['studentinformationsheet']=='1')
{
echo "Yes";
}
else
{
echo "No";
}
?>
</td>
<td width="89">
<?php if ($row['schoolapplication']=='1')
{
echo "Yes";
}
else
{
echo "No";
}
?>
</td>
<td width="118">
<?php if ($row['professionalreco']=='1')
{
echo "Yes";
}
else
{
echo "No";
}
?>
</td>
<td width="119">
<?php if ($row['reasonableaccom']=='1')
{
echo "Yes";
}
else
{
echo "No";
}
?>
</td>
<td width="58">
<?php if ($row['driverlicesnce']=='1')
{
echo "Yes";
}
else
{
echo "No";
}
?>
</td>
<td width="91">
<?php if ($row['highschooldip']=='1')
{
echo "Yes";
}
else
{
echo "No";
}
?>
</td>
</tr>
<?php } ?>
</table>
</body>
</html>

If you want to sort your select box by id, then definitely use "ORDER BY" as Pachonk says above.
Also, I've noticed that your PHP below:
<option>
<? echo $options ?>
</option>
is outputting:
<option>
<option value="Some id">some text
<option value="some other id">some other text
... repeat for all options
</option>
You should remove the first from around the PHP (you are placing an tag in the php echo statement anyway) and place the tag in your php code above so that every has an
like:
$result = mysql_query($sql) or die (mysql_error());
while ($row = mysql_fetch_array($result))
{
$id=$row["id"];
$course=$row["coursename"];
$options.="<OPTION VALUE=\"$id\">".$course . "</option>";
^^^^^^^^^^^^^ added these
}
?>
// removed <option>
<? echo $options ?>
// removed </option>
</select>
Of course, you could just have the php in the while clause output your option right there and then too.
Hope that helps in some way.

Related

Style is not being applied except for the first row from sql query in html code

I'm retrieving all the rows from sql database through a query, and i'm using a loop to get all the rows
I have a style.css file which i'm specifying a style for the , however, only the first row is receiving the style and the style is not being applied to the rest.
<!DOCTYPE html>
<html>
<link href="styles.css" rel="stylesheet">
<head>
<title>IIACSS search center</title>
</head>
<body>
<form method="post">
<label>Search</label>
<input type="text" name="search">
<input type="submit" name="submit">
</form>
<?php
$con = new PDO("mysql:host=localhost;dbname=data",'root','admin123');
if (isset($_POST["submit"])) {
$str = $_POST["search"];
$sth = $con->prepare("SELECT * FROM `questions` WHERE question LIKE '%$str%'");
$sth->setFetchMode(PDO:: FETCH_OBJ);
$sth -> execute();
?>
<table>
<tr>
<th>Project</th>
<th>Question</th>
<th>Sample</th>
</tr>
<?php
for ($a=0;$row = $sth->fetch();$a++) {
?>
<tr>
<td><?php echo $row->Proj_Name; ?></td>
<td><?php echo $row->Question;?></td>
<td><?php echo $row->sample;?></td>
<br><br>
</tr>
</table>
<?php
}
}
?>
body {
background-color: white;
}
h1 {
color: black;
}
td {
color: black;
font-family: sans-serif;
}
th {
color: blue;
font-family: sans-serif;
}
It is not a CSS problem, it is because you are closing your </table> in the loop, so it is ending your table after the first tr.
Just remove it from the loop:
<?php
$con = new PDO("mysql:host=localhost;dbname=data",'root','admin123');
if (isset($_POST["submit"])) {
$str = $_POST["search"];
$sth = $con->prepare("SELECT * FROM `questions` WHERE question LIKE '%$str%'");
$sth->setFetchMode(PDO:: FETCH_OBJ);
$sth -> execute();
?>
<table>
<tr>
<th>Project</th>
<th>Question</th>
<th>Sample</th>
</tr>
<?php
for ($a=0;$row = $sth->fetch();$a++) {
?>
<tr>
<td><?php echo $row->Proj_Name; ?></td>
<td><?php echo $row->Question;?></td>
<td><?php echo $row->sample;?></td>
<br><br>
</tr>
<?php
} ?>
</table>
<?php }
?>

Can't check if data exists in database

I know i post a lot of problems lately, but this one problem which i couldn't solve for about 1 week, I tried million times to check if data exists but still nothing. Searched, checked same problem but didn't get a hint. So please tell me what is wrong here. I try everything on my own but if i meet a problem i can't solve for a while. I ask here for help.
<!DOCTYPE html>
<html>
<head>
<title>Test Title</title>
</head>
<body>
<?php
class Database{
public $db;
public function __construct($host,$username,$password,$dbname){
$this->db=new MySQLi($host,$username,$password,$dbname);
}
public function getData(){
$query="SELECT * FROM artisan";
$result=$this->db->query($query);
$users=array();
if($result->num_rows>0){
while($row=$result->fetch_assoc()){
$users[]=array(
"id"=>$row['id'],
"username"=>$row['username'],
"email"=>$row['email']
);
}
}else{
echo "No results found!";
}
return $users;
}
public function getContent(){
$query="SELECT * FROM content";
$result=$this->db->query($query);
$values=array();
if($result->num_rows>0){
while($row=$result->fetch_assoc()){
$values[]=array(
"title"=>$row['title'],
"body"=>$row['body']
);
}
}
return $values;
}
public function insertData(){
$title=$_POST['title'];
$query="INSERT INTO content(title,body) VALUES (?,?)";
$result=$this->db->query("SELECT COUNT(*) FROM content WHERE title=$title");
$stmt=$this->db->prepare($query);
$stmt->bind_param("ss",$_POST['title'],$_POST['body']);
if(!empty($_POST['title']) || !empty($_POST['body'])){
if($result->num_rows){
echo "User allready exists";
}else{
$stmt->execute();
}
}
}
private function validate(){
$query="SELECT title and body FROM contents";
$result=$this->db->query($query);
if($result){
echo "hi";
}
}
}
$database=new Database('localhost','root','','test');
$users=$database->getData();
$values=$database->getContent();
$database->insertData();
?>
<style type="text/css">
.container{
text-align:center;
}
table, th, td, {
border: 1px solid black;
}
table{
width:100%;
}
.content{
border:1px solid black;
}
</style>
<div class="container">
<?php
for($i=0;$i<count($values);$i++):
?>
<div class="content">
<?php echo $values[$i]["title"]."<br>"; ?>
<?php echo $values[$i]["body"]."<br>";?>
</div>
<?php endfor;?>
</div>
<table>
<colgroup>
<col span="2" style="background-color:red">
<col style="background-color:yellow">
</colgroup>
<tr>
<th>ID</th>
<th>username</th>
<th>email</th>
</tr>
<tr>
<td><?php foreach($users as $user)
echo $user['id']."<br>";
?></td>
<td><?php foreach($users as $user)
echo $user['username']."<br>";
?></td>
<td><?php foreach($users as $user)
echo $user['email']."<br>";
?></td>
</tr>
</table>
<form action="index.php" method="POST">
<input type="text" name="title">
<input type="text" name="body">
<input type="submit" name="submit">
</form>
</body>
</html>
"$result=$this->db->query("SELECT title FROM content WHERE title='$title'"); just changed everything to this and it worked,$result was returning false,so i tried to solve it but i guess adding ' ' to $title solved idk why. – DummyTarget 12 mins ago"
That's because $title is a string literal and it needs to be quoted. You really should use a prepared statement for everything though, and will help safeguard against a potential SQL injection.
Reference:
https://dev.mysql.com/doc/refman/5.5/en/string-literals.html
Debugging:
http://php.net/manual/en/mysqli.error.php
http://php.net/manual/en/function.error-reporting.php

I need to insert data into mysql from a dynamic table with selects created with PHP

Wondering if someone could help me update mysql table with data from a couple of selects created dynamically with PHP, I have created the following code, but it seems not to be working, really appreciate your help:
`include_once('../includes/connection.php');`
// Query that retrieves events
$con = "SELECT * FROM evenement WHERE approved = 'no' ORDER BY id";
$result = mysqli_query($connection, $con);
if($con){
$registry = mysqli_affected_rows($connection);
if($registry > 0){
echo '
<h1 align="center">Events pending approval</h1>
<br><table width="100%" align="center" border="0" border-spacing="2px" cellspacing="1" cellpadding="1">
<form action="approveReject.php" method="post" >
<tr bgcolor="#3333FF" style="color:white; font-family:Tahoma, Geneva, sans-serif; font-size:15px"">
<th align="center"><strong>Title</strong></th>
<th align="center"><strong>Details</strong></th>
<th align="center"><strong>Category</strong></th>
<th align="center"><strong>Start</strong></th>
<th align="center"><strong>End</strong></th>
<th align="center"><strong>All Day event?</strong></th>
<th align="center"><strong>Approved</strong></th>
</tr>';
$color = "1";
while($registry = mysqli_fetch_array($result, MYSQLI_ASSOC)){
if($color==1){
echo '<tr bgcolor="#F8F8F8" font-family:Tahoma, Geneva, sans-serif; font-size:15px">';
$color="2";
} else {
echo '<tr bgcolor="#dcdcdc" font-family:Tahoma, Geneva, sans-serif; font-size:15px">';
$color="1";
}
echo '
<form action="" method="post">
<input type="hidden" value="'.$registry['id'].'" name="id[]" id="id">
<th div align="center">'.$registry['title'].'
<td div align="center">'.$registry['details'].'
<td div align="center">'.$registry['category'].'
<td div align="center">'.$registry['start'].'
<td div align="center">'.$registry['end'].'
<td div align="center"><select name="allDay[]" id="allDay">
<option value="0">Select option</option>
<option value="1">Yes</option>
<option value="2">No</option>
</select>
<td div align="center"><select name="ap_re[]" id="ap_re">
<option value="0">Select option</option>
<option value="1">Yes</option>
<option value="2">No</option>
</select>
</tr>';
}
?>
<td><input type="submit" name="button" id="button" value="Submit" style="height:1.8em; width:7.3em;" /></td>
<?php
echo '</form>
';
echo '
</form>
</table>';
}
}else{
echo '<h1>There are no new requests to be approved</h1>';
}
if(isset($_POST['Submit'])){
if($_POST['allDay'] == 'Yes'){
$allDay = 'true';
}else{
$allDay = 'false';
}
$ap_re = $_POST['ap_re'];
$i = 0;
foreach($_POST['id'] as $id){
$udpate_qry = "UPDATE evenement SET allDay='".$allDay."', approved='".$ap_re."', WHERE id='".$id."'";
$result_udpate_qry = mysqli_query($connection, $update_qry);
$i++;
}
}
?>
I don't think that I have solved all your issues, but this might help you further.
<?php
include_once('../includes/connection.php');
if (isset($_POST['submit'])) {
if ($_POST['allDay'] == 'Yes') {
$allDay = 'true';
} else {
$allDay = 'false';
}
$ap_re = $_POST['ap_re'];
$id = $_POST['id'];
$i = 0;
while ($id) {
$update_qry = "UPDATE evenement SET allDay='" . $allDay . "', approved='".$ap_re[$i]. "' WHERE id='".$id[$i]."'";
echo var_dump($update_qry);
mysqli_query($connection, $update_qry) OR DIE(mysqli_error($connection));
$i++;
}
header('Location: http://www.stockoverflow.com/'); // INSERT approveReject.php ???
}
else {
// Query that retrieves events
$con = "SELECT * FROM evenement WHERE approved = 'no' ORDER BY id";
$result = mysqli_query($connection, $con) or die(mysqli_error($connection));
if ($result) {
$registry = mysqli_num_rows($result) or die (mysqli_error($connection));
if ($registry > 0) {
echo '
<h1 align="center">Events pending approval</h1>
<br><table width="100%" align="center" border="0" border-spacing="2px" cellspacing="1" cellpadding="1">
<form action="" method="POST" >
<tr bgcolor="#3333FF" style="color:white; font-family:Tahoma, Geneva, sans-serif; font-size:15px"">
<th align="center"><strong>Title</strong></th>
<th align="center"><strong>Details</strong></th>
<th align="center"><strong>Category</strong></th>
<th align="center"><strong>Start</strong></th>
<th align="center"><strong>End</strong></th>
<th align="center"><strong>All Day event?</strong></th>
<th align="center"><strong>Approved</strong></th>
</tr>';
$color = "1";
while ($registry = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
if ($color == 1) {
echo '<tr bgcolor="#F8F8F8" font-family:Tahoma, Geneva, sans-serif; font-size:15px">';
$color = "2";
} else {
echo '<tr bgcolor="#dcdcdc" font-family:Tahoma, Geneva, sans-serif; font-size:15px">';
$color = "1";
}
echo '
<input type="hidden" value="' . $registry['id'] . '" name="id[]" id="id">
<th div align="center">' . $registry['title'] . '
<td div align="center">' . $registry['details'] . '
<td div align="center">' . $registry['category'] . '
<td div align="center">' . $registry['start'] . '
<td div align="center">' . $registry['end'] . '
<td div align="center"><select name="allDay[]" id="allDay">
<option value="0">Select option</option>
<option value="1">Yes</option>
<option value="2">No</option>
</select>
<td div align="center"><select name="ap_re[]" id="ap_re">
<option value="0">Select option</option>
<option value="1">Yes</option>
<option value="2">No</option>
</select>
</tr>';
}
?>
<input type="submit" name="submit" id="button" value="Submit" style="height:1.8em; width:7.3em;" />
<?php
echo '</form>
';
echo '
</form>
</table>';
}
} else {
echo '<h1>There are no new requests to be approved</h1>';
}
}
?>
You should try this, your update syntax might causing you the problem
$udpate_qry = "UPDATE evenement
SET allDay='$allDay', approved='$ap_re'
WHERE id='$id'";
Mr. Radical, you are the man, I was able to update my table with your suggested code, I just needed to add $length = count($allDay) and then change the while loop to while(($id) && ($i < $length)) that made it. Really appreciate your help.

Send $_GET using php or jquery

my index.php page has something like this:
if(isset($_GET['page'])) {
switch ($_GET['page']) {
case 'login':
include('login.php');
break;
case 'log_out':
$_SESSION['loggedin'] = false;
include('loggedout.html');
break;
case 'profile':
include('profile.php');
break;
case 'contact_us':
include('contact.html');
break;
default:
include('home.html');
break;
}
} else {
include('home.html');
}
Now I can easily call on a new page when a link is pressed or form is submitted i.e:
Home Contact Us
but what if I had an all in one form... It would need to be able to send a request through PHP code to my index.php to show the logged in page or reshow the login.php page but I cant seem to find anything that would allow me to send a request in php script i.e:
<?php
if(..) {
send request to index.php i.e index.php?page=profile
}
?>
The reason I need this is my index.php uses divs as formatting and thus if I dont request pages through index.php they are displayed in a new html (i.e it doesnt have my navigation bars etc)
As per a comment by #HydraIO and others how would this be done using JQuery?
Here is some code:
lgoin.php:
<?php session_start(); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Login</title>
<meta http-equiv="content-type"
content="text/html; charset=iso-8859-1"
/>
<!--
//thanks http://decoraora.com/fototapete_115x175.php for the image http://decoraora.com/fototapete/images/664.jpg -->
<style type="text/css">
.table {
background: url("bg.jpg") no-repeat;
width: 600px;
height: 250px;
}
</style>
</head>
<body>
<?php
if (isset($_POST['formLogin'])) {
$un = $_POST['username'];
$pwd = $_POST['password'];
if($pwd!=''&&$un!='') {
if (md5($pwd)==='29ef52e7563626a96cea7f4b4085c124') {
$_SESSION['loggedin'] = true;
$_SESSION['login_error'] = 0;
$_SESSION['username'] = $un;
$_SESSION['password'] = $pwd;
//include('profile.php');
//header("Location: index.php/page=profile");
//header("Location: http://localhost/index.php?index.php?page=profile");
} else {
$_SESSION['login_error'] = 1;
echo 'this will dipslay in its own page';
//include('login.php');
}
}else {
$_SESSION['login_error'] = 2;
echo 'display in its own page';
//include('login.php');
?>
<form action="login.php" method="post">
<table bgcolor="white" align="center" border="1px" width="50%" style="height: 40%;">
<tr>
<td colspan="2" align="center">Login for CC Chat</td>
</tr>
<tr>
<td>Username:</td>
<td>Password:</td>
</tr>
<tr>
<td><input type="text" name="username" maxlength="50"/></td>
<td><input type="password" name="password" maxlength="50"/></td>
</tr>
<tr>
<td>Forgot your password?</td>
<td><input type="submit" name="formLogin" value="Login"/></td>
</tr>
<?php
if(isset($_SESSION['login_error'])) {
switch ($_SESSION['login_error']) {
case 1:
echo '<tr>';
echo '<td>';
echo '<font color="red">Password does not match</font>';
echo '</td>';
echo '</tr>';
$_SESSION['login_error']=0;
break;
case 2:
echo '<tr>';
echo '<td>';
echo '<font color="red">Password and/or Username cannot be empty</font>';
echo '</td>';
echo '</tr>';
$_SESSION['login_error']=0;
break;
default:
break;
}
}
?>
<tr>
<td colspan="2" align="center">© CopyLeft David Kroukamp 11039662 2013 - 2015</td>
</tr>
</table>
</form>
<?php
}
}else {
?>
<form action="login.php" method="post">
<table bgcolor="white" align="center" border="1px" width="50%" style="height: 40%;">
<tr>
<td colspan="2" align="center">Login for CC Chat</td>
</tr>
<tr>
<td>Username:</td>
<td>Password:</td>
</tr>
<tr>
<td><input type="text" name="username" maxlength="50"/></td>
<td><input type="password" name="password" maxlength="50"/></td>
</tr>
<tr>
<td>Forgot your password?</td>
<td><input type="submit" name="formLogin" value="Login"/></td>
</tr>
<?php
if(isset($_SESSION['login_error'])) {
switch ($_SESSION['login_error']) {
case 1:
echo '<tr>';
echo '<td>';
echo '<font color="red">Password does not match</font>';
echo '</td>';
echo '</tr>';
$_SESSION['login_error']=0;
break;
case 2:
echo '<tr>';
echo '<td>';
echo '<font color="red">Password and/or Username cannot be empty</font>';
echo '</td>';
echo '</tr>';
$_SESSION['login_error']=0;
break;
default:
break;
}
}
?>
<tr>
<td colspan="2" align="center">© CopyLeft David Kroukamp 11039662 2013 - 2015</td>
</tr>
</table>
</form>
<?php
}
?>
</body>
</html>
index.php:
<?php session_start(); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>CC Chat</title>
<meta
http-equiv="content-type" content="text/html; charset=iso-8859-1"
/>
<!--
Thanks too http://stackoverflow.com/a/7851347/1133011 for the help
on layout which acts more like frames but avoids them and using divs. As frames have
known draw backs see here http://stackoverflow.com/questions/4600648/frames-with-php we
should thus rather use include in php
!-->
<style type="text/css" media="screen">
/* Generic pane rules */
body { margin: 0 }
.row, .col { overflow: hidden; position: absolute; }
.row { left: 0; right: 0; }
.col { top: 0; bottom: 0; }
.scroll-x { overflow-x: auto; }
.scroll-y { overflow-y: auto; }
.header.row { height: 75px; top: 0; background-color: #E5E5E5; }
.menu.row { height: 75px; top: 75px; background-color: #EDEDED; }
.body.row { top: 150px; bottom: 50px; background-color: #F5F5F5; }
.footer.row { height: 75px; bottom: 0; background-color: #EBEBEB; }
A:link {text-decoration: none}
A:visited {text-decoration: none}
A:active {text-decoration: none}
A:hover {font-size:24; font-weight:bold; color: red;}
</style>
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
</head>
<body>
<div class="header row">
<?php
include("header.html");
?>
</div>
<div class="body row scroll-y">
<?php
if(isset($_GET['page'])) {
switch ($_GET['page']) {
case 'login':
include('login.php');
break;
case 'log_out':
$_SESSION['loggedin'] = false;
include('loggedout.html');
break;
case 'profile':
include('profile.php');
break;
case 'contact_us':
include('contact.html');
break;
default:
include('home.html');
break;
}
} else {
include('home.html');
}
?>
</div>
<div class="menu row">
<?php
include("nav.php");
?>
</div>
<div class="footer row">
<?php
include("footer.php");
?>
</div>
</body>
</html>
UPDATED with JQuery attempt in if statement which checks password hash:
<?php session_start(); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Login</title>
<meta http-equiv="content-type"
content="text/html; charset=iso-8859-1"
/>
<!--
//thanks http://decoraora.com/fototapete_115x175.php for the image http://decoraora.com/fototapete/images/664.jpg -->
<style type="text/css">
.table {
background: url("bg.jpg") no-repeat;
width: 600px;
height: 250px;
}
</style> <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
</head>
<body>
<?php
if (isset($_POST['formLogin'])) {
$un = $_POST['username'];
$pwd = $_POST['password'];
if($pwd!=''&&$un!='') {
if (md5($pwd)==='29ef52e7563626a96cea7f4b4085c124') {
$_SESSION['loggedin'] = true;
$_SESSION['login_error'] = 0;
$_SESSION['username'] = $un;
$_SESSION['password'] = $pwd;
//include('profile.php');
//header("Location: index.php/page=profile");
//header("Location: http://localhost/index.php?index.php?page=profile");
//exit();
echo "<script type='text/javascript'>";
echo "$.get('index.php?page=profile', function(data) {
$('.body.row').html(data);
alert('Load was performed.');
});";
echo "</script>";
} else {
$_SESSION['login_error'] = 1;
echo 'this will dipslay in its own page';
//include('login.php');
}
}else {
$_SESSION['login_error'] = 2;
echo 'display in its own page';
//include('login.php');
?>
<form action="login.php" method="post">
<table bgcolor="white" align="center" border="1px" width="50%" style="height: 40%;">
<tr>
<td colspan="2" align="center">Login for CC Chat</td>
</tr>
<tr>
<td>Username:</td>
<td>Password:</td>
</tr>
<tr>
<td><input type="text" name="username" maxlength="50"/></td>
<td><input type="password" name="password" maxlength="50"/></td>
</tr>
<tr>
<td>Forgot your password?</td>
<td><input type="submit" name="formLogin" value="Login"/></td>
</tr>
<?php
if(isset($_SESSION['login_error'])) {
switch ($_SESSION['login_error']) {
case 1:
echo '<tr>';
echo '<td>';
echo '<font color="red">Password does not match</font>';
echo '</td>';
echo '</tr>';
$_SESSION['login_error']=0;
break;
case 2:
echo '<tr>';
echo '<td>';
echo '<font color="red">Password and/or Username cannot be empty</font>';
echo '</td>';
echo '</tr>';
$_SESSION['login_error']=0;
break;
default:
break;
}
}
?>
<tr>
<td colspan="2" align="center">© CopyLeft David Kroukamp 11039662 2013 - 2015</td>
</tr>
</table>
</form>
<?php
}
}else {
?>
<form action="login.php" method="post">
<table bgcolor="white" align="center" border="1px" width="50%" style="height: 40%;">
<tr>
<td colspan="2" align="center">Login for CC Chat</td>
</tr>
<tr>
<td>Username:</td>
<td>Password:</td>
</tr>
<tr>
<td><input type="text" name="username" maxlength="50"/></td>
<td><input type="password" name="password" maxlength="50"/></td>
</tr>
<tr>
<td>Forgot your password?</td>
<td><input type="submit" name="formLogin" value="Login"/></td>
</tr>
<?php
if(isset($_SESSION['login_error'])) {
switch ($_SESSION['login_error']) {
case 1:
echo '<tr>';
echo '<td>';
echo '<font color="red">Password does not match</font>';
echo '</td>';
echo '</tr>';
$_SESSION['login_error']=0;
break;
case 2:
echo '<tr>';
echo '<td>';
echo '<font color="red">Password and/or Username cannot be empty</font>';
echo '</td>';
echo '</tr>';
$_SESSION['login_error']=0;
break;
default:
break;
}
}
?>
<tr>
<td colspan="2" align="center">© CopyLeft David Kroukamp 11039662 2013 - 2015</td>
</tr>
</table>
</form>
<?php
}
?>
</body>
</html>
I see the alert but am redirected to a blank html like before (even than before at least the form was reloaded
It sounds like you might want to evaluate all the data inside your index page first and do a page redirect by setting a header inside php to the same index page with the get query updated to whatever you want it to be.
header("Location: http://www.example.com/index.php?page=profile");
exit();
make sure you don't output any html with echo or any other way before you do the redirect. If your script heavily uses echo before that you can look into outputbuffering your echos.
PHP is a server-side language, and not dynamic. You want to use javascript (jQuery in my example).
$_GET = <?php echo json_encode($_GET); ?>;
if($_GET['val'] == some_value){
$.get(index.php?getvar=getval, {
complete: function(){
$('#div').val("YOUR OUTPUT"); // *
}
});
}
*This can be any code you feel like, you could replace the div with the entire php file if you chose to

How to sort rows of HTML table that are called from MySQL

I know it's such a basic thing, but a Google search hasn't shown me how to re-sort the rows after clicking the th links.
I've got this:
<table border="1">
<tr>
<th>Type:</th>
<th>Description:</th>
<th>Recorded Date:</th>
<th>Added Date:</th>
</tr>
<?php
while($row = mysql_fetch_array($result)){
?>
<tr>
<td><?php echo $row['type'] ?></td>
<td><?php echo $row['description'] ?></td>
<td><?php echo $row['recorded_date'] ?></td>
<td><?php echo $row['added_date'] ?></td>
</tr>
<br />
<?php
}
mysql_close();
?>
</table>
I need to be able to click type and sort alphabetically, and click on either Recorded Date or Added Date and sort by date. I see that I need to have the MySQL queries do this, but do I set them up as conditionals with a href tags?
The easiest way to do this would be to put a link on your column headers, pointing to the same page. In the query string, put a variable so that you know what they clicked on, and then use ORDER BY in your SQL query to perform the ordering.
The HTML would look like this:
<th>Type:</th>
<th>Description:</th>
<th>Recorded Date:</th>
<th>Added Date:</th>
And in the php code, do something like this:
<?php
$sql = "SELECT * FROM MyTable";
if ($_GET['sort'] == 'type')
{
$sql .= " ORDER BY type";
}
elseif ($_GET['sort'] == 'desc')
{
$sql .= " ORDER BY Description";
}
elseif ($_GET['sort'] == 'recorded')
{
$sql .= " ORDER BY DateRecorded";
}
elseif($_GET['sort'] == 'added')
{
$sql .= " ORDER BY DateAdded";
}
$>
Notice that you shouldn't take the $_GET value directly and append it to your query. As some user could got to MyPage.php?sort=; DELETE FROM MyTable;
That's actually pretty easy, here's a possible approach:
<table>
<tr>
<th>
Type:
</th>
<th>
Description:
</th>
<th>
Recorded Date:
</th>
<th>
Added Date:
</th>
</tr>
</table>
<?php
$orderBy = array('type', 'description', 'recorded_date', 'added_date');
$order = 'type';
if (isset($_GET['orderBy']) && in_array($_GET['orderBy'], $orderBy)) {
$order = $_GET['orderBy'];
}
$query = 'SELECT * FROM aTable ORDER BY '.$order;
// retrieve and show the data :)
?>
That'll do the trick! :)
A SIMPLE TABLE SORT PHP CODE:
(the simple table for several values processing and sorting, using this sortable.js script )
<html><head>
<script src="sorttable.js"></script>
<style>
tbody tr td {color:green;border-right:1px solid;width:200px;}
</style>
</head><body>
<?php
$First = array('a', 'b', 'c', 'd');
$Second = array('1', '2', '3', '4');
if (!empty($_POST['myFirstvalues']))
{ $First = explode("\r\n",$_POST['myFirstvalues']); $Second = explode("\r\n",$_POST['mySecondvalues']);}
?>
</br>Hi User. PUT your values</br></br>
<form action="" method="POST">
projectX</br>
<textarea cols="20" rows="20" name="myFirstvalues" style="width:200px;background:url(untitled.PNG);position:relative;top:19px;Float:left;">
<?php foreach($First as $vv) {echo $vv."\r\n";}?>
</textarea>
The due amount</br>
<textarea cols="20" rows="20" name="mySecondvalues" style="width:200px;background:url(untitled.PNG);Float:left;">
<?php foreach($Second as $vv) {echo $vv."\r\n";}?>
</textarea>
<input type="submit">
</form>
<table class="sortable" style="padding:100px 0 0 300px;">
<thead style="background-color:#999999; color:red; font-weight: bold; cursor: default; position:relative;">
<tr><th>ProjectX</th><th>Due amount</th></tr>
</thead>
<tbody>
<?php
foreach($First as $indx => $value) {
echo '<tr><td>'.$First[$indx].'</td><td>'.$Second[$indx].'</td></tr>';
}
?>
</tbody>
<tfoot><tr><td>TOTAL = <b>111111111</b></td><td>Still to spend = <b>5555555</b></td></tr></tfoot></br></br>
</table>
</body>
</html>
source: php sortable table
//this is a php file
<html>
<head>
<style>
a:link {color:green;}
a:visited {color:purple;}
A:active {color: red;}
A:hover {color: red;}
table
{
width:50%;
height:50%;
}
table,th,td
{
border:1px solid black;
}
th,td
{
text-align:center;
background-color:yellow;
}
th
{
background-color:green;
color:white;
}
</style>
<script type="text/javascript">
function working(str)
{
if (str=="")
{
document.getElementById("tump").innerHTML="";
return;
}
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("tump").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","getsort.php?q="+str,true);
xmlhttp.send();
}
</script>
</head>
<body bgcolor="pink">
<form method="post">
<select name="sortitems" onchange="working(this.value)">
<option value="">Select</option>
<option value="Id">Id</option>
<option value="Name">Name</option>
<option value="Email">Email</option>
<option value="Password">Password</option>
</select>
<?php
$connect=mysql_connect("localhost","root","");
$db=mysql_select_db("test1",$connect);
$sql=mysql_query("select * from mine");
echo "<center><br><br><br><br><table id='tump' border='1'>
<tr>
<th>Id</th>
<th>Name</th>
<th>Email</th>
<th>Password</th>
</tr>";
echo "<tr>";
while ($row=mysql_fetch_array($sql))
{?>
<td><?php echo "$row[Id]";?></td>
<td><?php echo "$row[Name]";?></td>
<td><?php echo "$row[Email]";?></td>
<td><?php echo "$row[Password]";?></td>
<?php echo "</tr>";
}
echo "</table></center>";?>
</form>
<br>
<div id="tump"></div>
</body>
</html>
------------------------------------------------------------------------
that is another php file
<html>
<body bgcolor="pink">
<head>
<style>
a:link {color:green;}
a:visited {color:purple;}
A:active {color: red;}
A:hover {color: red;}
table
{
width:50%;
height:50%;
}
table,th,td
{
border:1px solid black;
}
th,td
{
text-align:center;
background-color:yellow;
}
th
{
background-color:green;
color:white;
}
</style>
</head>
<?php
$q=$_GET['q'];
$connect=mysql_connect("localhost","root","");
$db=mysql_select_db("test1",$connect);
$sql=mysql_query("select * from mine order by $q");
echo "<table id='tump' border='1'>
<tr>
<th>Id</th>
<th>Name</th>
<th>Email</th>
<th>Password</th>
</tr>";
echo "<tr>";
while ($row=mysql_fetch_array($sql))
{?>
<td><?php echo "$row[Id]";?></td>
<td><?php echo "$row[Name]";?></td>
<td><?php echo "$row[Email]";?></td>
<td><?php echo "$row[Password]";?></td>
<?php echo "</tr>";
}
echo "</table>";?>
</body>
</html>
that will sort the table using ajax
This is the most simple solution that use:
// Use this as first line upon load of page
$sort = $_GET['s'];
// Then simply sort according to that variable
$sql="SELECT * FROM tracks ORDER BY $sort";
echo '<tr>';
echo '<td>Title<td>';
echo '<td>Album<td>';
echo '<td>Artist<td>';
echo '<td>Count<td>';
echo '</tr>';
It depends on nature of your data. The answer varies based on its size and data type. I saw a lot of SQL solutions based on ORDER BY. I would like to suggest javascript alternatives.
In all answers, I don't see anyone mentioning pagination problem for your future table. Let's make it easier for you. If your table doesn't have pagination, it's more likely that a javascript solution makes everything neat and clean for you on the client side. If you think this table will explode after you put data in it, you have to think about pagination as well. (you have to go to first page every time when you change the sorting column)
Another aspect is the data type. If you use SQL you have to be careful about the type of your data and what kind of sorting suites for it. For example, if in one of your VARCHAR columns you store integer numbers, the sorting will not take their integer value into account: instead of 1, 2, 11, 22 you will get 1, 11, 2, 22.
You can find jquery plugins or standalone javascript sortable tables on google. It worth mentioning that the <table> in HTML5 has sortable attribute, but apparently it's not implemented yet.

Categories