Need help with repeating/looping in PHP - php

I have this block of PHP and HTML:
<table width="57%" border="0" class="tabela_master" id="aps-cat">
<tr>
<?php
while ($row = $db->sql_fetchrow($result_1))
{
echo '<td width="33%" rowspan="12" align="center"><img src="../images/' . $row['picture'] . '" /></td>';
echo '<td width="20%" align="right" class="tabela_master" style="font-weight: bold">Emri / Mbiemri:</td>';
echo '<td width="1%"> </td>';
echo '<td width="46%" class="tabela_master">' . $row['firstname'] . " " . $row['lastname'] . '</td>';
echo '</tr>';
echo '<tr>';
echo '<td align="right" class="tabela_master" style="font-weight: bold">Gjinia:</td>';
echo '<td> </td>';
echo '<td class="tabela_master">' . $row['gender'] . '</td>';
echo '</tr>';
echo '<tr>';
echo '<td align="right" class="tabela_master" style="font-weight: bold">Datelindja:</td>';
echo '<td> </td>';
echo '<td class="tabela_master">' . $row['birthday'] . '</td>';
echo '</tr>';
echo '<tr>';
echo '<td align="right" class="tabela_master" style="font-weight: bold">Adresa / Lokacioni:</td>';
echo '<td> </td>';
echo '<td class="tabela_master">' . $row['location'] . '</td>';
echo '</tr>';
echo '<tr>';
echo '<td align="right" class="tabela_master" style="font-weight: bold">Telefoni:</td>';
echo '<td> </td>';
echo '<td class="tabela_master">' . $row['telephone'] . '</td>';
echo '</tr>';
echo '<tr>';
echo '<td align="right" class="tabela_master" style="font-weight: bold">Email adresa:</td>';
echo '<td> </td>';
echo '<td class="tabela_master">' . $row['email'] . '</td>';
echo '</tr>';
echo '<tr>';
echo '<td align="right" class="tabela_master" style="font-weight: bold">Interesi:</td>';
echo '<td> </td>';
echo '<td class="tabela_master">' . $row['occupation'] . '</td>';
echo '</tr>';
echo '<tr>';
echo '<td align="right" class="tabela_master" style="font-weight: bold"> </td>';
echo '<td> </td>';
echo '<td class="tabela_master"> </td>';
echo '</tr>';
echo '<tr>';
echo '<td align="right" class="tabela_master" style="font-weight: bold"> </td>';
echo '<td> </td>';
echo '<td class="tabela_master"> </td>';
echo '</tr>';
echo '<tr>';
echo '<td align="right" class="tabela_master" style="font-weight: bold"> </td>';
echo '<td> </td>';
echo '<td class="tabela_master"> </td>';
echo '</tr>';
echo '<tr>';
echo '<td align="right" class="tabela_master" style="font-weight: bold"> </td>';
echo '<td> </td>';
echo '<td class="tabela_master"> </td>';
echo '</tr>';
echo '<tr>';
echo '<td align="right" class="tabela_master" style="font-weight: bold"> </td>';
echo '<td> </td>';
echo '<td class="tabela_master"> </td>';
}
//$db->sql_freeresult($result_1);
?>
</tr>
</table>
Now what I want is multiple records shown in the page. The web currently looks like this:
And what I want would look like this:
So my table would provide all my results from my MySQL query which looks like this:
$sql_1 = 'SELECT id, firstname, lastname, birthday, location, occupation, gender, telephone, email, picture
FROM pinkmoon_users ORDER BY `id` DESC LIMIT 1';
$result_1 = $db->sql_query($sql_1) or die($db->sql_error());

Remove the LIMIT 1 in your SQL statement
$sql_1 = 'SELECT id, firstname, lastname, birthday, location, occupation, gender, telephone, email, picture
FROM pinkmoon_users ORDER BY `id` DESC ';
$result_1 = $db->sql_query($sql_1) or die($db->sql_error());

Your query retrieves only the last record from the database. If you want more, you should change the LIMIT 1 to (for example) LIMIT 10 or remove the limit contraint entirely.

Related

My search didn't work

My search won't work and I don't know why. I would like to search for clients or tickets in my database. When I click on search while the SEARCH field ( input) is empty I get all clients BUT when I type some thing into the field I get nothing. Here is the code :
<table class="table table-bordered" id="datatable">
<thead>
<tr>
<th width="6%">name</th>
<th width="6%">prenom</th>
<th width="12%">adresse</th>
<th width="3%">codepost</th>
<th width="6%">ville</th>
<th width="5%">telephone</th>
<th width="8%">email</th>
<th width="6%">type</th>
<th width="5%">Action</th>
</tr>
</thead>
<tbody>
<?php
require 'database.php';
$db = Database::connect();
$statement = $db->query("SELECT * FROM clients WHERE 'prenom' LIKE '%" . $_POST['prenom'] . "%' ORDER BY client_id DESC" );
while($item = $statement->fetch())
{
echo '<tr>';
// echo '<td>'. $item['id'] . '</td>';
echo '<td>'. $item['name'] . '</td>';
echo '<td>'. $item['prenom'] . '</td>';
echo '<td>'. $item['adress'] . '</td>';
echo '<td>'. $item['codepost'] . '</td>';
echo '<td>'. $item['ville'] . '</td>';
echo '<td>'. $item['telephone'] . '</td>';
echo '<td>'. $item['email'] . '</td>';
echo '<td>'. $item['type'] . '</td>';
echo '<td width=150>';
echo '<a class="btn btn-default " href="view.php?id='.$item['client_id'].'"><span class="glyphicon glyphicon-eye-open"></span></a>';
echo ' ';
echo '<a class="btn btn-primary" href="update.php?id='.$item['client_id'].'"><span class="glyphicon glyphicon-pencil"></span></a>';
echo ' ';
echo '<a class="btn btn-danger" href="deleteclient.php?id='.$item['client_id'].'"><span class="glyphicon glyphicon-remove"></span> </a>';
echo '</td>';
echo '</tr>';
}
Database::disconnect();
?>
</tbody>
</table>
i finaly did it .. thanks to who helped me (
Sam Onela ) here is the code if some one need ``it := `
name
prenom
adresse
codepost
ville
telephone
email
type
Action
prepare(" SELECT * FROM clients WHERE name LIKE ? ");
$req->execute(array('%' .$searchq. '%')) ;
$count = $req->rowCount();
echo " $count Résulta(s) trouvé pour <strong >$searchq></strong><hr/> " ;
while($data = $req->fetch()) {
echo '<tr>';
echo '<td>'. $data['name'] . '</td>';
echo '<td>'. $data['prenom'] . '</td>';
echo '<td>'. $data['adress'] . '</td>';
echo '<td>'. $data['codepost'] . '</td>';
echo '<td>'. $data['ville'] . '</td>';
echo '<td>'. $data['telephone'] . '</td>';
echo '<td>'. $data['email'] . '</td>';
echo '<td>'. $data['type'] . '</td>';
echo '<td width=150>';
echo '<a class="btn btn-default " href="view.php?id='.$data['client_id'].'"><span class="glyphicon glyphicon-eye-open"></span></a>';
echo ' ';
echo '<a class="btn btn-primary" href="update.php?id='.$data['client_id'].'"><span class="glyphicon glyphicon-pencil"></span></a>';
echo ' ';
echo '<a class="btn btn-danger" href="deleteclient.php?id='.$data['client_id'].'"><span class="glyphicon glyphicon-remove"></span> </a>';
echo '</td>';
echo '</tr>';
}
}
Database::disconnect();
?>
</tbody>
</table>`

how do I update the value of each row without primary key?

I want to update the value in a certain row. But it seems impossible since this table (from database) doesn't have the primary key. It only have two foreign key in its column. What should I do ? please, any help ?
here is my code :
<table align= "center" border="1" cellspacing="0" cellpadding="2">
<?php
$strgettable="select tblUsrGrp.UsrGrpNama, tblmenu.MenuNama, tblmenuakses.MenuAkses
FROM tblUsrGrp
inner join tblMenuAkses
on tblUsrGrp.UsrGrpID = tblMenuAkses.usrgrpid
inner join tblMenu
on tblMenu.menuid = tblMenuAkses.menuid";
$varRecCount=0;
$rs=odbc_exec($dbconnVOT, $strgettable);
if($rs)
{
echo '<tr><td style=width:10%;>';
echo '<center><b> No. </b></center>';
echo '</td>';
echo '<td style=width:30%;>';
echo '<center><b> User Group </b></center>';
echo '</td>';
echo '<td style=width:20%;>';
echo '<center><b> Menu Name </b></center>';
echo '</td>';
echo '<td style=width:20%;>';
echo '<center><b> Menu Akses </b></center>';
echo '</td>';
echo '<td style=width:20%;>';
echo '<center><b> Update </b></center>';
echo '</td></tr>';
while (odbc_fetch_row($rs))
{
$varRecCount++;
echo '<tr><td>';
echo '<center> '.$varRecCount.'</center>';
echo '</td>';
echo '<td>';
echo '<center> '.odbc_result($rs,"UsrGrpNama").'</center>';
echo '</td>';
echo '<td>';
echo '<center> '.odbc_result($rs,"MenuNama").'</center>';
echo '</td>';
echo '<td>';
echo '<center> '.odbc_result($rs,"MenuAkses").'</center>';
echo '</td>';
echo '<td>';
echo '</td>';
echo '</td></tr>';
}
}
?>
</table>
<div style="text-align:center;">
<input type="button" name="btnUpdate" id="btnUpdate" onClick="javascript:sendData('updatedata');" value = "Update">
</div>
any help would be much appreciated . TQ

Get Similar rows count in mysql

I am working on a project in which a tutor can save its class timing. He can see his timing according to the days. I used the code
$qry = mysqli_query($con, 'select * from users left join time_slot on users.id=time_slot.u_id where users.id=' . $id);
echo '<table class="table_class" border="2">
<tr>
<th class="details">id</th>
<th class="details">Date</th>
<th class="details">start time</th>
<th class="details">End Time</th>
</tr>';
while ($row = mysqli_fetch_array($qry)) {
echo '<tr>';
echo '<td class="details">' . $row['id'] . '</td>';
echo '<td class="details">' . $row['name'] . '</td>';
echo '<td class="details">' . $row['day'] . '</td>';
echo '<td class="details">' . $row['time_from'] . '</td>';
echo '<td class="details">' . $row['time_to'] . '</td>';
echo '</tr>';
}
echo '</table>';
But It show the multiple time if a tutor have multiple class in same day.
I want to show if he has 2 or more class on similar day(Monday) then all time slot show in a single row. Same this for all days of the week. How can I do it?
You can use GROUP_CONCAT function for this. Assuming your ddl is something like that
create table users(id bigint, name varchar(50));
create table time_slot(id bigint, u_id bigint, day datetime, time_from time, time_to time);
the sql would be as follows:
select u.id,u.name, ts.day,
group_concat(ts.time_from, ' - ', ts.time_to ORDER BY ts.time_from, ts.time_to)
from users u left outer join time_slot ts on u.id = ts.u_id
group by u.id, u.name, ts.day
order by u.name, ts.day
See fiddle.
I have did with some temp values.
if you want in same way to impliment then it is usefull for you.
copy the code and check here http://phpfiddle.org/
$obj1['id']='1';
$obj1['name']='a1';
$obj1['day']='asdadh';
$obj1['time_from']='1';
$obj1['time_to']='1';
$obj2['id']='2';
$obj2['name']='a2';
$obj2['day']='asdad';
$obj2['time_from']='1';
$obj2['time_to']='1';
$obj3['id']='3';
$obj3['name']='a2';
$obj3['day']='asdad';
$obj3['time_from']='1';
$obj3['time_to']='1';
$arr = Array();
$arr[]=$obj1;
$arr[]=$obj2;
$arr[]=$obj3;
echo '<table class="table_class" border="2">';
echo '<tr>';
echo '<th class="details">id</th>';
echo '<th class="details">name</th>';
echo '<th class="details">day</th>';
echo '<th class="details">start time</th>';
echo '<th class="details">End Time</th>';
echo '</tr>';
foreach($arr as $row)
{
echo '<tr>';
echo '<td class="details">' . $row['id'] . '</td>';
echo '<td class="details">' . $row['name'] . '</td>';
echo '<td class="details">' . $row['day'] . '</td>';
echo '<td class="details">' . $row['time_from'] . '</td>';
echo '<td class="details">' . $row['time_to'] . '</td>';
echo '</tr>';
}
echo '</table>';
echo "<br><br><br><br><br><br><br>";
$dates=Array();
$count=0;
foreach($arr as $id=>$row){
$val = $row['day'];
$key = array_search($val,$dates);
if(is_numeric($key)){
$arr[$key]['day']=$dates[$key].','.$val;
unset($arr[$id]);
}else{
$dates[$count]=$val;
}
$count++;
}
// new table
echo '<table class="table_class" border="2">';
echo '<tr>';
echo '<th class="details">id</th>';
echo '<th class="details">name</th>';
echo '<th class="details">day</th>';
echo '<th class="details">start time</th>';
echo '<th class="details">End Time</th>';
echo '</tr>';
foreach($arr as $row)
{
echo '<tr>';
echo '<td class="details">' . $row['id'] . '</td>';
echo '<td class="details">' . $row['name'] . '</td>';
echo '<td class="details">' . $row['day'] . '</td>';
echo '<td class="details">' . $row['time_from'] . '</td>';
echo '<td class="details">' . $row['time_to'] . '</td>';
echo '</tr>';
}
echo '</table>';

SQL query doesn't give the right result

I have a form where people can search the database for four values:
Location, Period, Day and Service. I always do not get the results that I want.
If I use AND, people need to fill in everything. If I use OR I get the complete database. I want to be able to search the database for those one to 4 things. Is there a way how I can do this?
Is there maybe a way to check which fields are filled in, and that the query is automatically changed with the filled in fields?
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Zoeken</title>
</head>
<body>
<p><img src="add.png" width="20px" height="20px"/> | <img src="search.png" width="20px" height="20px"/> | <img src="number.png" width="20px" height="20px"/> </p>
<form action="" method="post">
<div>
<table>
<tr><td><strong>Locatie: </strong></td><td><input type="text" name="Locatie" value="" /></td> </tr>
<tr><td><strong>Periode: </strong></td><td><input type="text" name="Periode" value="" /></td> </tr>
<tr><td><strong>Dag: </strong></td><td><input type="text" name="Dag" value="" /></td> </tr>
<tr><td><strong>Dienst: </strong></td> <td><input type="text" name="Dienst" value="" /></td></tr>
<tr><td></td><td><input type="submit" name="zoeken" value="Zoeken"></td></tr>
</table>
</div>
</form>
<p><img src="add.png" width="20px" height="20px"/> | <img src="search.png" width="20px" height="20px"/> | <img src="number.png" width="20px" height="20px"/> </p>
</body>
</html>
<?php
if (isset($_POST['zoeken']))
{
include('connect-db.php');
$Locatie = $_POST['Locatie'];
$Periode = $_POST['Periode'];
$Dag = $_POST['Dag'];
$Dienst = $_POST['Dienst'];
// get results from database
$result = mysql_query("SELECT * FROM WMC_DeLijn WHERE Locatie='$Locatie' ANY Periode='$Periode' ANY Dag='$Dag'ANY Dienst='$Dienst' ")
or die(mysql_error());
// display data in table
echo "<h2>Resultaten:</h2><p>";
echo "<table border='1' cellpadding='10'>";
echo "<table><tr><th>ID</th><th>Locatie</th><th>Periode</th><th>Dag</th><th>Dienst</th><th>Delen</th><th>Geleed</th><th>Start 1</th><th>Eind 1</th><th>Start 2</th><th>Eind 2</th><th>Lijnen</th></tr>";
// loop through results of database query, displaying them in the table
while($row = mysql_fetch_array( $result )) {
// echo out the contents of each row into a table
echo "<tr>";
echo '<td align="center">' . $row['id'] . '</td>';
echo '<td align="center">' . $row['Locatie'] . '</td>';
echo '<td align="center">' . $row['Periode'] . '</td>';
echo '<td align="center">' . $row['Dag'] . '</td>';
echo '<td align="center">' . $row['Dienst'] . '</td>';
echo '<td align="center">' . $row['Delen'] . '</td>';
echo '<td align="center">' . $row['Geleed'] . '</td>';
echo '<td align="center">' . $row['Start1'] . '</td>';
echo '<td align="center">' . $row['Eind1'] . '</td>';
echo '<td align="center">' . $row['Start2'] . '</td>';
echo '<td align="center">' . $row['Eind2'] . '</td>';
echo '<td align="center">' . $row['Lijnen'] . '</td>';
//Link to edit record
echo '<td align="center"><img src="edit.png" width="20px" height="20px"/></td>';
// Link to delete record
echo '<td align="center"><img src="delete.png" width="20px" height="20px"/></td>';
//Link to Add Event to Google Calendar
echo '<td align="center"><img src="proceed.png" width="20px" height="20px"/></td>';
echo "</tr>";
}}
// close table>
echo "</table>";
?>
You can either build the query string dynamically, only adding WHERE clause statements if the parameter is not falsy, or add conditions in the SQL itself like so: WHERE (col = ? OR '' = ?) AND (col2 = ? OR '' = ?).
Change your query to have either a AND condition or a OR condition like
SELECT * FROM WMC_DeLijn
WHERE Locatie='$Locatie'
AND Periode='$Periode'
AND Dag='$Dag'
AND ienst='$Dienst';
(OR)
SELECT * FROM WMC_DeLijn
WHERE Locatie='$Locatie'
OR Periode='$Periode'
OR Dag='$Dag'
OR ienst='$Dienst';

Datatables and Codeigniter

I'm using the datatables plugin to format my tables with my data.
I make the call to my model from my controller to gather all the data.
I send the data array to the view file.
I start a table tag in my view with the datatable class which calls the plugin to turn the regular html table into a datatables table.
I check to verify that there is data in the array in the view and then if there is I do a foreach loop to create the table rows. If there is not data then I echo out a string of no data found.
My issue is when there is data it works fine however when there is not data I receive Requested Unknown Parameter 1 from the data source for row 0 error message.
I've seen others that have come across this same issue before however all the other posts I have seen have explained that they have used the datatables json data while specifically working with the datatables plugin.
Any ideas on how to prevent this error message from displaying.
<table class="dynamicTable table table-striped table-bordered table-condensed">
<thead>
<tr>
<th style="width: 1%;" class="uniformjs" id="checkboxth"><input type="checkbox" /></th>
<th class="center">ID</th>
<th>Name</th>
<th class="center">Date</th>
<th class="center" id="created_updated"></th>
<th class="right" id="actions">Actions</th>
</tr>
</thead>
<tbody>
<?php
//vardump($themes);
if (isset($themes) && is_array($themes))
{
foreach ($themes AS $theme)
{
echo '<tr class="selectable">';
echo '<td class="center uniformjs"><input type="checkbox" /></td>';
echo '<td class="center">' . $theme->id . '</td>';
echo '<td>' . $theme->name . '</td>';
if ($theme->updated_at != '0000-00-00 00:00:00')
{
echo '<td class="center" style="width: 80px;">' . date('d M Y', strtotime($theme->created_at)) . '</td>';
echo '<td class="center" style="width: 80px;"><span class="label label-block label-inverse">updated</span></td>';
}
else
{
echo '<td class="center" style="width: 80px;">' . date('d M Y', strtotime($theme->created_at)) . '</td>';
echo '<td class="center" style="width: 80px;"><span class="label label-block label-important">created</span></td>';
}
echo '<td class="center" style="width: 60px;">';
echo '<i></i>';
echo '<i></i>';
echo '</td>';
echo '</tr>';
}
}
else
{
echo '<tr>';
echo '<td class="center" colspan="7">There are no themes. Select Add a New Theme.</td>';
echo '</tr>';
}
?>
</tbody>
</table>
This is the code that came with the theme.
/* DataTables */
if ($('.dynamicTable').size() > 0)
{
$('.dynamicTable').dataTable({
"sPaginationType": "bootstrap",
"sDom": "<'row-fluid'<'span6'l><'span6'f>r>t<'row-fluid'<'span6'i><'span6'p>>",
"oLanguage": {
"sLengthMenu": "_MENU_ records per page"
}
});
}
Hi Please remove else condition from your code.Then your code will look like:
<table class="dynamicTable table table-striped table-bordered table-condensed">
<thead>
<tr>
<th style="width: 1%;" class="uniformjs" id="checkboxth"><input type="checkbox" /></th>
<th class="center">ID</th>
<th>Name</th>
<th class="center">Date</th>
<th class="center" id="created_updated"></th>
<th class="right" id="actions">Actions</th>
</tr>
</thead>
<tbody>
<?php
//vardump($themes);
if (isset($themes) && is_array($themes))
{
foreach ($themes AS $theme)
{
echo '<tr class="selectable">';
echo '<td class="center uniformjs"><input type="checkbox" /></td>';
echo '<td class="center">' . $theme->id . '</td>';
echo '<td>' . $theme->name . '</td>';
if ($theme->updated_at != '0000-00-00 00:00:00')
{
echo '<td class="center" style="width: 80px;">' . date('d M Y', strtotime($theme->created_at)) . '</td>';
echo '<td class="center" style="width: 80px;"><span class="label label-block label-inverse">updated</span></td>';
}
else
{
echo '<td class="center" style="width: 80px;">' . date('d M Y', strtotime($theme->created_at)) . '</td>';
echo '<td class="center" style="width: 80px;"><span class="label label-block label-important">created</span></td>';
}
echo '<td class="center" style="width: 60px;">';
echo '<i></i>';
echo '<i></i>';
echo '</td>';
echo '</tr>';
}
}
/* else
{
echo '<tr>';
echo '<td class="center" colspan="7">There are no themes. Select Add a New Theme.</td>';
echo '</tr>';
}*/
?>
</tbody>
</table>
Hope it will works.Because datatable automatically handle no data in table.

Categories