I don't understand, I have a variable $i which is not incrementing in my loop.
while ($ligne = mysqli_fetch_array($resu2,MYSQL_ASSOC)) {
echo '<tr>';
for($i = 1; $nbjour >= $i; $i++)
{
while ($ligne2 = mysqli_fetch_array($resu3,MYSQL_ASSOC)) {
$element=$ligne2['jourIndisponibilite'];
echo "1er ".$element;
echo "2eme ".$ligne['idSPProfessionnel'];
echo ' ';
echo $i;
echo ' ';
if ($ligne2['jourIndisponibilite']==$i && $ligne['idSPPprofessionnel']==$ligne2['idSpProfessionnel']){
echo '<td height=25>';
echo 'X';
echo '</td>';
}
}
echo '<td height=20></td>';
}
echo '</tr>';
}
echo "</table>";
echo '</br>';
?>
Related
I am getting website design ideas from a website API.
Currently my setup displays these designs in a table with only one column, I have over 40 items that need to be displayed in the table, but id prefer to display it in 3 columns. I have tried using i=0 ++ things etc but couldnt work out where to put it.
Here is my code so far:
<?php
$output = json_decode($output);
curl_close($ch);
echo '<table border="1">';
foreach($output as $template) {
echo '<tr>';
echo '<td>' . $template->template_name . '</td>';
echo '</tr>' . '<tr>';
echo '<td><img src="' . $template->thumbnail_url. '">' . '</td>';
echo '<tr>';
echo '<td><form method="GET" action=' . $_SERVER['PHP_SELF'] . '>';
echo '<input type="hidden" name="template_id" value="' . $template->template_id . '">';
echo '<input type="url" name="original_url" placeholder="Existing Site URL">'.'<br />';
echo '<input type="email" name="email" placeholder="Your e-mail" required>'.'<br />';
echo '<button type="submit">Choose Site</button>';
echo '</form></td>';
echo '</tr>';
}
echo '</table>';
}
?>
The output includes all of the website templates.
I am putting it into rows.The first row contains the name of the template, the second the picture of the website template and the third a form which I am working on.
My question is how can I nest the loop so that it does 3 columns, each row with 3 different designs?
Many thanks in advance
The common idea may be as the following, for example:
<?php
$array = [0, 1, 2, 3, 4, 5, 6, 7, 8];
$i = 1;
echo "<table>";
foreach ($array as $arr) {
if ($i % 3 === 0) {
echo "<tr class='class_3'>";
} else if ($i % 2 === 0) {
echo "<tr class='class_2'>";
} else {
echo "<tr class='class_1'>";
}
echo '<td>'.($i++).'</td>';
echo '<td>'.($i++).'</td>';
echo '<td>'.($i++).'</td>';
echo "</tr>";
//$i++; for 123 456
}
echo '</table>';
?>
Update: a more specific example:
<?php
$assoc_array = json_decode('
[
{"template_id": "1","template_name":"A", "preview_url":"url_1"},
{"template_id": "2","template_name":"B", "preview_url":"url_2"},
{"template_id": "3","template_name":"C", "preview_url":"url_3"},
{"template_id": "4","template_name":"D", "preview_url":"url_4"},
{"template_id": "5","template_name":"E", "preview_url":"url_5"},
{"template_id": "6","template_name":"F", "preview_url":"url_6"},
{"template_id": "7","template_name":"G", "preview_url":"url_7"},
{"template_id": "8","template_name":"H", "preview_url":"url_8"},
{"template_id": "9","template_name":"I", "preview_url":"url_9"},
{"template_id": "10","template_name":"J", "preview_url":"url_10"}
]
', true);
echo '<table>';
for($i = 0; $i < sizeof($assoc_array); $i++) {
if ($i % 3 === 0) {
echo "<tr class='class_3'>";
} else if ($i % 2 === 0) {
echo "<tr class='class_2'>";
} else {
echo "<tr class='class_1'>";
}
echo '<td>'.$assoc_array[$i]['template_name'].'</td>';
if(isset($assoc_array[++$i]['template_name'])) {
echo '<td>'.$assoc_array[$i]['template_name'].'</td>';
}
if(isset($assoc_array[++$i]['template_name'])) {
echo '<td>'.$assoc_array[$i]['template_name'].'</td>';
}
echo '</tr>';
}
echo '</table>';
?>
You'll get:
A B C
D E F
G H I
J
Thanks everyone who contributed. My answer is now finished.
The final code was:
<?php
$output = json_decode($output);
curl_close($ch);
$i = 0;
echo '<table border="1" width="80%">';
echo '<tr>';
foreach($output as $template) {
echo '<td><img src="' . $template->thumbnail_url. '" height="300" width="460">' .'<br />';
echo '<form method="GET" action=' . $_SERVER['PHP_SELF'] . '>';
echo '<input type="hidden" name="template_id" value="' . $template->template_id . '">';
echo '<input type="url" name="original_url" placeholder="Existing Site URL">'.'<br />';
echo '<input type="email" name="email" placeholder="Your e-mail" required>'.'<br />';
echo '<button type="submit">Choose Site</button>';
echo '</form></td>';
$i++;
if($i == 3) { // three items in a row. Edit this to get more or less items on a row
echo '</tr>'.'<tr>';
$i = 0;
}
}
echo '</tr>';
echo '</table>';
}
?>
My controller function:
function index()
{
$data['user_clicks'] = $this->common_model->get_userClicks();
$this->load->view('common/header');
$this->load->view('content_pages/user_clicks',$data);
$this->load->view('common/footer');
}
My model
function get_userClicks()
{
$this->db->select('agent_id,click_type,click_count');
$this->db->select_sum('click_count');
$this->db->from('daily_clicks');
$this->db->group_by('click_type,agent_id');
$this->db->order_by('agent_id');
$q = $this->db->get();
return $q->result_array();
}
this is i am getting, what i want is to get the click_count of phone click, email click,offline click with respect to their columns and their total clicks is already done.
the data base image is the last one klindly help me get through it.
Thank you
Try this,
$resarr=count($resultarray);
<table>
<tr>
<th>Agent_id</th>
<th>phone_click</th>
<th>email_click</th>
<th>offline_click</th>
<th>total_click</th>
</tr>
for($i=1; $i<100; $i++){ //agent id
for($j=1; $j<=$resarr; $j++) //number of records
{
echo "<tr>";
if($arr['user_clicks'][$j]['agent_id'] == $i)
{
if(!empty($arr['user_clicks'][$j]['agent_id']){
echo "<td">
echo $arr['user_clicks'][$j]['agent_id'];
echo "</td>";
}
if($arr['user_clicks'][$j]['click_type']=='phone'){
echo "<td">
echo $arr['user_clicks'][$j]['click_type'];
echo "</td>";
}
else
{
echo "<td">
echo '';
echo "</td>";
}
if($arr['user_clicks'][$j]['click_type']=='email'){
echo "<td">
echo $arr['user_clicks'][$j]['click_type'];
echo "</td>";
}else
{
echo "<td">
echo '';
echo "</td>";
}
if($arr['user_clicks'][$j]['click_type']=='offline'){
echo "<td">
echo $arr['user_clicks'][$j]['click_type'];
echo "</td>";
}
else
{
echo "<td">
echo '';
echo "</td>";
}
if(!empty($arr['user_clicks'][$j]['click_count']){
echo "<td">
echo $arr['user_clicks'][$j]['click_count'];
echo "</td>";
}
else
{
echo "<td">
echo '';
echo "</td>";
}
}
echo "</tr>";
}
}
I am trying to query the table with the $_GET parameter, but when I add the parameter nothing is ordering by specified value. Is there something wrong with how I have written this if statement?
if (isset($_GET['sort'])) {
$sorting = $_GET['sort'];
$result = $mysqli->query("SELECT * FROM routes ORDER BY $sorting");
}
else if (!isset($_GET['sort'])) {
$result = $mysqli->query("SELECT * FROM routes _");
}
if ($result->num_rows != 0) {
$total_results = $result->num_rows;
$total_pages = ceil($total_results / $per_page);
if (isset($_GET['page']) && is_numeric($_GET['page']) && $_GET['page'] <= $total_pages) {
$show_page = $_GET['page'];
if ($show_page > 0 && $show_page <= $total_pages) {
$start = ($show_page - 1) * $per_page;
$end = $start + $per_page;
} else {
$start = 0;
$end = $per_page;
}
} else {
$start = 0;
$end = $per_page;
}
echo "<form action='../processors/delete.php' method='post'";
echo "<br/><table class='table table-striped table-bordered' id='basic-datatable' border='0' cellpadding='10'>";
echo "<thead><tr> <th>#</th>";
echo "<th><a href='adminviewer.php?sort=route_id'>Route ID</a></th>";
echo "<th><a href='adminviewer.php?sort=route_title'>Route</a></th>";
echo "<th><a href='adminviewer.php?sort=route_price'>Price</a></th>";
echo "<th><a href='adminviewer.php?sort=route_payment'>Payment</a></th>";
echo "<th><a href='adminviewer.php?sort=route_net'>Weekly Net</a></th>";
echo "<th><a href='adminviewer.php?sort=route_city'>City</a></th>";
echo "<th><a href='adminviewer.php?sort=route_state'>State</a></th>";
echo "<th><a href='adminviewer.php?sort=route_remarks'>Remarks</a></th>";
echo "</tr></thead>";
echo "<tbody>";
// Check for Sorting Request
for ($i = $start; $i < $end; $i++) {
if ($i == $total_results) {
break;
}
$result->data_seek($i);
$row = $result->fetch_row();
echo "<tr>";
echo '<td><input class="checked-box" type="checkbox" name="selec-row[]" value="' . $row[0] . '"></td>';
echo '<td>' . $row[1] . '</td>';
echo '<td>' . $row[2] . '</td>';
echo '<td>' . $row[3] . '</td>';
echo '<td>' . $row[4] . '</td>';
echo '<td>' . $row[5] . '</td>';
echo '<td>' . $row[6] . '</td>';
echo '<td>' . $row[7] . '</td>';
echo '<td>' . $row[8] . '</td>';
echo "</tr>";
}
echo "</tbody>";
echo "</table>";
echo "<input class='del-btn' type='submit' value='Delete'>";
echo "</form>";
} else {
echo "No results to display!";
}
echo '<div class="col-md-12" style="text-align: center;position:relative;top:-25px;">';
echo "Showing 1 to " . $per_page . " of " . $total_results . " routes";
echo '</div>';
/* echo '<div class="col-md-6" style="text-align:right;">';
echo '<ul class="paginated-btns">';
for ($i = 1; $i <= $total_pages; $i++)
{
if (isset($_GET['page']) && $_GET['page'] == $i)
{
echo "<li><a href='adminviewer.php?page=$i&page_sel=$per_page'>$i</a></li>";
}
else
{
echo "<li><a href='adminviewer.php?page=$i&page_sel=$per_page'>$i</a></li>";
}
}
echo '</ul>';
*/
echo "</div>";
echo '</div>';
echo '</div>';
echo '</div>';
echo '<div class="pagination">';
if (!isset($_GET['page'])) {
$_GET['page'] = 1;
}
if ($_GET['page'] > $per_page) {
$next_page = $i - 1;
echo "<div class='pag-btn'><a href='adminviewer.php?page=$next_page&page_sel=$per_page'><- Previous</a></div>";
} else {
echo "<div class='pag-btn'>Previous</div>";
}
if ($_GET['page'] < $total_pages) {
$next_page = $_GET['page'] + 1;
echo "<div class='pag-btn'><a href='adminviewer.php?page=$next_page&page_sel=$per_page'>Next -></a></div>";
} else {
echo "<div class='pag-btn'>Next</div>";
}
echo '</div>';
I'm learning PHP so I can imagine something is wrong with the logic, but I can't seem to figure it out.
The values of the $_GET['sort'] are columns of the table; for example: route_id, route_name, route_price, ...
This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 7 years ago.
I have been trying to tackle this problem for many hours.
i feel its something as simple as how the if statement should work.
Bellow is my code that i am trying to add to: And underneath that code i have typed the php that i tried to use: But i keep getting errors regarding to the structure of the if statement?
Additionally I believe my code itself is at fault, as this error only happens when I insert my code. To confirm this is the pseudo:
If sort parameter exists in URL then use the value when selecting the table. Otherwise use the non order sql.
-----------Trying to edit----------
if (isset($_GET[sort])) {
$sorting = $_GET['sort'];
$result = $mysqli->query("SELECT * FROM routes ORDER BY $sorting");
}
else {
$result = $mysqli->query("SELECT * FROM routes");
}
-----------Trying to edit----------
if ($result->num_rows != 0)
{
$total_results = $result->num_rows;
$total_pages = ceil($total_results / $per_page);
if (isset($_GET['page']) && is_numeric($_GET['page']) && $_GET['page'] <= $total_pages )
{
$show_page = $_GET['page'];
if ($show_page > 0 && $show_page <= $total_pages)
{
$start = ($show_page -1) * $per_page;
$end = $start + $per_page;
}
else
{
$start = 0;
$end = $per_page;
}
}
else
{
$start = 0;
$end = $per_page;
}
echo "<form action='../processors/delete.php' method='post'";
echo "<br/><table class='table table-striped table-bordered' id='basic-datatable' border='0' cellpadding='10'>";
echo "<thead><tr> <th>#</th>";
echo "<th><a href='adminviewer.php?sort=route_id'>Route ID</a></th>";
echo "<th><a href='adminviewer.php?sort=route_name'>Route</a></th>";
echo "<th><a href='adminviewer.php?sort=route_price'>Price</a></th>";
echo "<th><a href='adminviewer.php?sort=route_payment'>Payment</a></th>";
echo "<th><a href='adminviewer.php?sort=route_net'>Weekly Net</a></th>";
echo "<th><a href='adminviewer.php?sort=route_city'>City</a></th>";
echo "<th><a href='adminviewer.php?sort=route_state'>State</a></th>";
echo "<th><a href='adminviewer.php?sort=route_remarks'>Remarks</a></th>";
echo "</tr></thead>";
echo "<tbody>";
for ($i = $start; $i < $end; $i++)
{
if ($i == $total_results) { break; }
$result->data_seek($i);
$row = $result->fetch_row();
echo "<tr>";
echo '<td><input class="checked-box" type="checkbox" name="selec-row[]" value="' . $row[0] . '"></td>';
echo '<td>' . $row[1] . '</td>';
echo '<td>' . $row[2] . '</td>';
echo '<td>' . $row[3] . '</td>';
echo '<td>' . $row[4] . '</td>';
echo '<td>' . $row[5] . '</td>';
echo '<td>' . $row[6] . '</td>';
echo '<td>' . $row[7] . '</td>';
echo '<td>' . $row[8] . '</td>';
echo "</tr>";
}
echo "</tbody>";
echo "</table>";
echo "<input class='del-btn' type='submit' value='Delete'>";
echo "</form>";
}
else
{
echo "No results to display!";
}
echo '<div class="col-md-12" style="text-align: center;position:relative;top:-25px;">';
echo "Showing 1 to " . $per_page . " of " . $total_results . " routes";
echo '</div>';
echo "</div>";
echo '</div>';
echo '</div>';
echo '</div>';
echo '<div class="pagination">';
if (!isset($_GET['page'])) {
$_GET['page'] = 1;
}
if ($_GET['page'] > $per_page) {
$next_page = $i - 1;
echo "<div class='pag-btn'><a href='adminviewer.php?page=$next_page&page_sel=$per_page'><- Previous</a></div>";
}
else {
echo "<div class='pag-btn'>Previous</div>";
}
if ($_GET['page'] < $total_pages) {
$next_page = $_GET['page'] + 1;
echo "<div class='pag-btn'><a href='adminviewer.php?page=$next_page&page_sel=$per_page'>Next -></a></div>";
}
else {
echo "<div class='pag-btn'>Next</div>";
}
echo '</div>';
}
else -----syntax error, unexpected '}'----
{
echo "Error: " . $mysqli->error;
}
if(isset($_GET['sort'])) {
$sorting = sanitize_input($_GET['sort']);
$result = mysqli->query("SELECT * FROM table ORDER BY $sorting");
} else {
$result = mysqli->query("SELECT * FROM table ORDER BY id");
}
I have looked over the code and i cant see issues with the statement. PHP is telling me errors like unexpected } and when i remove that its like unexpected else etc.
Please share some light on this that would be great, as well as any general structural improvements if you see any :)
Error Code: Parse error: syntax error, unexpected '}'
I'm still learning so my critiquing.
Elevant
I think you have an else without an opening if statement, check this out (in last lines of the script), made the test with your edit and get the same
<?php
// -----------Trying to edit----------
if (isset( $_GET[sort]) )
{
$sorting = $_GET['sort'];
$result = $mysqli->query("SELECT * FROM routes ORDER BY $sorting");
} else {
$result = $mysqli->query("SELECT * FROM routes");
}
// -----------Trying to edit----------
if ($result->num_rows != 0)
{
$total_results = $result->num_rows;
$total_pages = ceil($total_results / $per_page);
if (isset($_GET['page']) && is_numeric($_GET['page']) && $_GET['page'] <= $total_pages )
{
$show_page = $_GET['page'];
if ($show_page > 0 && $show_page <= $total_pages)
{
$start = ($show_page -1) * $per_page;
$end = $start + $per_page;
} else {
$start = 0;
$end = $per_page;
}
} else {
$start = 0;
$end = $per_page;
}
echo "<form action='../processors/delete.php' method='post'";
echo "<br/><table class='table table-striped table-bordered' id='basic-datatable' border='0' cellpadding='10'>";
echo "<thead><tr> <th>#</th>";
echo "<th><a href='adminviewer.php?sort=route_id'>Route ID</a></th>";
echo "<th><a href='adminviewer.php?sort=route_name'>Route</a></th>";
echo "<th><a href='adminviewer.php?sort=route_price'>Price</a></th>";
echo "<th><a href='adminviewer.php?sort=route_payment'>Payment</a></th>";
echo "<th><a href='adminviewer.php?sort=route_net'>Weekly Net</a></th>";
echo "<th><a href='adminviewer.php?sort=route_city'>City</a></th>";
echo "<th><a href='adminviewer.php?sort=route_state'>State</a></th>";
echo "<th><a href='adminviewer.php?sort=route_remarks'>Remarks</a></th>";
echo "</tr></thead>";
echo "<tbody>";
for ($i = $start; $i < $end; $i++)
{
if ($i == $total_results)
{
break;
}
$result->data_seek($i);
$row = $result->fetch_row();
echo "<tr>";
echo '<td><input class="checked-box" type="checkbox" name="selec-row[]" value="' . $row[0] . '"></td>';
echo '<td>' . $row[1] . '</td>';
echo '<td>' . $row[2] . '</td>';
echo '<td>' . $row[3] . '</td>';
echo '<td>' . $row[4] . '</td>';
echo '<td>' . $row[5] . '</td>';
echo '<td>' . $row[6] . '</td>';
echo '<td>' . $row[7] . '</td>';
echo '<td>' . $row[8] . '</td>';
echo "</tr>";
}
echo "</tbody>";
echo "</table>";
echo "<input class='del-btn' type='submit' value='Delete'>";
echo "</form>";
} else {
echo "No results to display!";
}
echo '<div class="col-md-12" style="text-align: center;position:relative;top:-25px;">';
echo "Showing 1 to " . $per_page . " of " . $total_results . " routes";
echo '</div>';
echo "</div>";
echo '</div>';
echo '</div>';
echo '</div>';
echo '<div class="pagination">';
if ( !isset($_GET['page']) )
{
$_GET['page'] = 1;
}
if ($_GET['page'] > $per_page)
{
$next_page = $i - 1;
echo "<div class='pag-btn'><a href='adminviewer.php?page=$next_page&page_sel=$per_page'><- Previous</a></div>";
} else {
echo "<div class='pag-btn'>Previous</div>";
}
if ($_GET['page'] < $total_pages)
{
$next_page = $_GET['page'] + 1;
echo "<div class='pag-btn'><a href='adminviewer.php?page=$next_page&page_sel=$per_page'>Next -></a></div>";
} else {
echo "<div class='pag-btn'>Next</div>";
}
echo '</div>';
/* The same thing, no opening if statement. Sure this is not the error?
}
else -----syntax error, unexpected '}'----
{
echo "Error: " . $mysqli->error;
}
**/
?>
Hope this helps :)
Here is my associative array:
$req_data1[]=array(
'depart1'=>$_REQUEST['to'],
'd_time1'=>$d_time5,
'stop'=>"",
'leave_stop'=>"",
'arrival1'=>$_REQUEST['from'],
'a_time1'=>$end_time5,
'price1'=>intval($final_price),
'air_line'=>"xxxxx");
Here is my sorting algorithm:
foreach ($req_data as $key => $row) {
$depart[$key] = $row['depart'];
$d_time[$key] = $row['d_time'];
$stop[$key] = $row['stop'];
$leave_stop[$key] = $row['leave_stop'];
$arrival[$key] = $row['arrival'];
$a_time[$key] = $row['a_time'];
$price[$key] = $row['price'];
}
array_multisort($price,SORT_ASC, $req_data);
I am displaying the data after sorting:
foreach($req_data as $key=>$row) {
echo "</br>";
foreach($row as $key2=>$row2){
echo $row2;
}
}
My problem now is that I want to put that array into an HTML table but dont know how. This is my code which I tried so far but its not working:
$cols = 5;
echo "<table border=\"5\" cellpadding=\"10\">";
for ($r=0; $r < count($row2); $r++) {
echo "<tr>";
for ($c=0; $c<$cols; $c++) {
?> <td> <?php $input[$r+$c] ?> </td> <?php
}
echo "</tr>";
$r += $c;
}
echo "</table>";
?>
Could any one tell me what is wrong with my code, or how I can display this sorted data into a table? Thanks.
Why not just modify the loop you already use to display the data?
echo "<table>";
foreach($req_data as $key=>$row) {
echo "<tr>";
foreach($row as $key2=>$row2){
echo "<td>" . $row2 . "</td>";
}
echo "</tr>";
}
echo "</table>";
$toOutput = '<table>';
$showHeader = true;
$memberData = $reportObj->getMemberData();
while($row = mysql_fetch_assoc($memberData))
{
$toOutput .= '<tr>';
//Outputs a header if nessicary
if($showHeader)
{
$keys = array_keys($row);
for($i=0;$i<count($keys);$i++)
{
$toOutput .= '<td>' . $keys[$i] . '</td>';
}
$toOutput .= '</tr><tr>';
$showHeader = false;
}
//Outputs the row
$values = array_values($row);
for($i=0;$i<count($values);$i++)
{
$toOutput .= '<td>' . $values[$i] . '</td>';
}
$toOutput .= '</tr>';
}
$toOutput .= '</table>';
echo 'Test page';
echo $toOutput;
Sorry for the necro, but I was actually looking to see if there was a build-in function for this as I was writing this.
function DumpTable($array_assoc) {
if (is_array($array_assoc)) {
echo '<table class="table">';
echo '<thead>';
echo '<tr>';
list($table_title) = $array_assoc;
foreach ($table_title as $key => &$value):
echo '<th>' . $key . '</th>';
endforeach;
echo '</tr>';
echo '</thead>';
foreach ($array_assoc as &$master):
echo '<tr>';
foreach ($master as &$slave):
echo '<td>' . $slave . '</td>';
endforeach;
echo '</tr>';
endforeach;
echo '</table>';
return;
}
}
echo "<table border=\"5\" cellpadding=\"10\">";
for ($r=0; $r < count($row2); $r++) {
echo "<tr>";
for ($c=0; $c<$cols; $c++) { ?>
<td> <?php $input[$r+$c] ?> </td>
<?php }
echo "</tr>";
$r += $c;
}
echo "</table>";?>
Try something like this
echo "<table>";
for($r=0;$r<count($row2);$r++){
echo "<tr>";
for($c=0;$c<$cols;$c++){
echo "<td>".[VARIABLE YOU WANT TO PRINT]."</td>";
}
echo "</tr>";
}
echo "</table>";
you can try the following:
echo "The associative array<br>";
$computer=array("brand"=>"hp", "price"=>"800", "cpu"=>"core i7");
$keys=array_keys($computer);
echo "<table><tr>";
foreach($keys as $row){
echo "<th style=\"border: solid 2px green\">".$row."</th>";
}echo "</tr><tr>";
foreach($computer as $col){
echo "<td style=\"border: solid 2px blue\">".$col."</td>";
}echo "</tr></table>";