Select all from table and display result mysql - php

<?php
$query_announcement = mysql_query("SELECT * FROM announcement");
$get_announcement = mysql_fetch_assoc($query_announcement);
?>
<?php
if(mysql_num_rows($query_announcement) > 0){
$row = mysql_num_rows($query_announcement);
for($x = 1; $x<= $row; $x++){
$html = '<tr>';
$html .= '<td>'.$get_announcement['id'].'</td>';
$html .= '<td>'.$get_announcement['announce_name'].'</td>';
$html .= '<td>'.$get_announcement['announce_description'].'</td>';
$html .= '<td>'.$get_announcement['announce_location'].'</td>';
$html .= '<td>'.date('M d,Y', strtotime($get_announcement['date_start'])).'</td>';
$html .= '<td>'.date('M d,Y', strtotime($get_announcement['date_end'])).'</td>';
$html .= '<td>'.date('M d,Y', strtotime($get_announcement['date_added'])).'</td>';
$html .= '<td width="15%">
<button type="button" class="btn btn-default" title="Edit"><span class="fa fa-pencil"></span></button>
<button type="button" class="btn btn-default" title="Delete" ><span class="fa fa-trash-o"></span></button>
</td>';
$html .= '</tr>';
echo $html;
}
}
else{
echo '<tr><td colspan="8" align="center"><h3>No Announcement</h3></td></tr>';
}
?>
/this is my code but when i run it i only get the announcement with the same id, not all announcement that i created. please help thanks

You are looping the announcement but are not then pulling the next announcement.
for($x = 1; $x<= $row; $x++){
//Pull next record
$row = mysql_num_rows($query_announcement);
If you change it to the code above the for loop will start. The data will be pulled, displayed and then it will loop back around again.

your r fetching data only one time
for($x = 1; $x<= $row; $x++){
$get_announcement= mysql_fetch_assoc($query_announcement);

Try this: You need to loop through the rows to and use the each row to display them.
<?php
$query_announcement = mysql_query("SELECT * FROM announcement");
$get_announcement = mysql_fetch_assoc($query_announcement);
?>
<?php
if($get_announcement){
while($row = mysql_fetch_array($get_announcement)) {
$html = '<tr>';
$html .= '<td>'.$row['id'].'</td>';
$html .= '<td>'.$row['announce_name'].'</td>';
$html .= '<td>'.$row['announce_description'].'</td>';
$html .= '<td>'.$row['announce_location'].'</td>';
$html .= '<td>'.date('M d,Y', strtotime($row['date_start'])).'</td>';
$html .= '<td>'.date('M d,Y', strtotime($row['date_end'])).'</td>';
$html .= '<td>'.date('M d,Y', strtotime($row['date_added'])).'</td>';
$html .= '<td width="15%">
<button type="button" class="btn btn-default" title="Edit"><span class="fa fa-pencil"></span></button>
<button type="button" class="btn btn-default" title="Delete" ><span class="fa fa-trash-o"></span></button>
</td>';
$html .= '</tr>';
echo $html;
}
}
else{
echo '<tr><td colspan="8" align="center"><h3>No Announcement</h3></td></tr>';
}

Related

Color table row by status

I wanna do if in by my status field.
if my status is "blocked" so the font background color will be in red and if the status is "authorized" so in green.
how should i do that?
foreach($data as $row)
{
$output .= '
<tr>
<td>'.$row->client_name.'</td>
<td>'.$row->adrress.'</td>
<td>'.$row->occupation.'</td>
<td>'.$row->payeee.'</td>
<td>'.$row->status.'</td>
</tr>
';
}
Try this
foreach($data as $row)
{
$output .= '
<tr>
<td>'.$row->client_name.'</td>
<td>'.$row->adrress.'</td>
<td>'.$row->occupation.'</td>
<td>'.$row->payeee.'</td>
#if($row->status == "blocked")
<td> <button type="button" class="btn btn-danger">blocked</button> </td>
#elseif($row->status == "authorized")
<td> <button type="button" class="btn btn-success">authorized</button> </td>
#endif
</tr>
OR
foreach($data as $row){
$output .= '
<tr>
<td>'.$row->client_name.'</td>
<td>'.$row->adrress.'</td>
<td>'.$row->occupation.'</td>
<td>'.$row->payeee.'</td>';
if($row->status == "bad"){
$output .= '<td style="background-color:red;">'.$row->status.'</td>';
} else if($row->status == "good") {
$output .= '<td style="background-color:green;">'.$row->status.'</td>';
}
$output .= '</tr>';
}
You shouldn't really be dealing with view logic in your controller.
But to do what you want in the controller
foreach($data as $row){
$output .= '
<tr>
<td>'.$row->client_name.'</td>
<td>'.$row->adrress.'</td>
<td>'.$row->occupation.'</td>
<td>'.$row->payeee.'</td>';
if($row->status == "bad"){
$output .= '<td style="background-color:red;">'.$row->status.'</td>';
} else if($row->status == "good") {
$output .= '<td style="background-color:green;">'.$row->status.'</td>';
}
$output .= '</tr>';
}
However what you should do is handle it in your blade file. Also you probably want to create styles in your style sheet and apply classes rather than inline the css.
this is the code that worked
foreach($data as $row){
$output .= '
<tr>
<td>'.$row->client_name.'</td>
<td>'.$row->adrress.'</td>
<td>'.$row->occupation.'</td>
<td>'.$row->payeee.'</td>';
if($row->status == "bad"){
$output .= '<td style="background-color:red;">'.$row->status.'</td>';
} else if($row->status == "good") {
$output .= '<td style="background-color:green;">'.$row->status.'</td>';
}
$output .= '</tr>';
}

if checkbox checked enable his textarea

in this php code i have foreach loop create textarea with checkbox for each statement
i wanna to enable textarea if his checkbox is checked
but they have same id
<?php
foreach($Arows as $row){
echo '<tr class="row">
<th class="col-sm-12"><h4>'.$row['Type_A'].'</h4></th>
</tr>';
$stmt3 = $con->prepare("SELECT * FROM sous_analyse WHERE Id_A =".$row['Id_A']);
$stmt3->execute();
$SArows = $stmt3->fetchAll();//Analyse rows
foreach($SArows as $Srow){
echo '<tr class="row">
<td class="col-sm-4">'.$Srow['Parameter'].'</td>
<td class=" col-sm-5"><input type="text" class="form-control col-xl-12 border-secondary" name="Fonctionnalite" placeholder="RESULTATS" required=""></td>
<td class="col-sm-3 text-center">'.$Srow['Normal'].'</td>
</tr>';
}
echo '<tr class="row">
<th class="col-sm-12">
<div class="input-group">
<span class="input-group-addon form-check">
<span class="checkbox-custom checkbox-default">
<input type="checkbox" class="icheckbox-grey" id="inputUnchecked" name="inputUnchecked">
<label for="inputUnchecked"></label>
</span>
</span>
<textarea class="form-control" id="textareaDefault" rows="3" style="margin-top: 0px; margin-bottom: 0px; height: 60px;" disabled></textarea>
</div>
</th>
</tr>';
}
?>
i use this script but is working in first textarea
<script >
$(document).ready(function() {
$("#inputUnchecked").on('click', function() {
if($(this).is(':checked')){
$("#textareaDefault").prop('disabled',false);
} else {
$("#textareaDefault").prop('disabled',true);
}
alert("fdg");
})
});
</script>
Expanding on my comment, you will want to adjust your PHP. This is easily done with a counter ($c).
<?php
foreach($Arows as $row){
$html = "";
$html .= '<tr class="row">'."\r\n";
$html .= '<th class="col-sm-12"><h4>'.$row['Type_A'].'</h4></th>'."\r\n";
$html .= '</tr>'."\r\n";
$stmt3 = $con->prepare("SELECT * FROM sous_analyse WHERE Id_A =".$row['Id_A']);
$stmt3->execute();
$SArows = $stmt3->fetchAll();
$c = 1;
foreach($SArows as $Srow){
$html .= '<tr class="row">'."\r\n";
$html .= '<td class="col-sm-4">'.$Srow['Parameter'].'</td>';
$html .= '<td class=" col-sm-5"><input type="text" class="form-control col-xl-12 border-secondary" name="Fonctionnalite" placeholder="RESULTATS" required=""></td>';
$html .= '<td class="col-sm-3 text-center">'.$Srow['Normal'].'</td>';
$html .= '</tr>';
}
$html .= '<tr class="row">';
$html .= '<th class="col-sm-12">';
$html .= '<div class="input-group">';
$html .= '<span class="input-group-addon form-check">';
$html .= '<span class="checkbox-custom checkbox-default">';
$html .= '<input type="checkbox" class="icheckbox-grey" id="inputUnchecked-$c" name="inputUnchecked">';
$html .= '<label for="inputUnchecked-$c"></label>';
$html .= '</span></span>';
$html .= '<textarea class="form-control" id="textareaDefault-$c" rows="3" style="margin-top: 0px; margin-bottom: 0px; height: 60px;" disabled></textarea>';
$html .= '</div></th></tr>';
echo $html;
$c++;
}
?>
Your JavaScript can then use the ID since it is unique or can simply be relative to nearby elements.
$(function() {
$("[id^='inputUnchecked-']").on('click', function() {
if($(this).is(':checked')){
$(this).parent().parent().find("[id^='textareaDefault-']").prop('disabled',false);
} else {
$(this).parent().parent().find("[id^='textareaDefault-']").prop('disabled',true);
}
alert("fdg");
})
});

Foreign Key In Php to get data from another table

function show_username($connect)
{
$output = '';
$query = "SELECT * from users";
$res = mysqli_query($connect, $query);
while($row = mysqli_fetch_array($res))
{
$output .= '<option value="'.$row["id"].'">'.$row["name"].'</option>';
{
}
return $output;
}
function show_offer($connect)
{
$output = '';
$query = "SELECT * FROM add_offer ORDER BY id DESC";
$res = mysqli_query($connect, $query);
while($row = mysqli_fetch_array($res))
{
$output .= '<div class="col-md-3 col-sm-4 col-lg-6">';
$output .= '<div class="panel panel-default">';
$output .= '<div class="panel-body">';
$output .= '<div style="padding:1px;float:left;font-weight:bold;">'.$row["part_no"].'</div>';
$output .= '<div style="padding:1px;float:left;">'.$row["make"].'</div>';
$output .= '<div style="padding:1px;float:left;">'.$row["date_code"].'</div>';
$output .= '<div style="padding:1px;float:left;">'.$row["qty"].'</div>';
$output .= '<br>';
$output .= '<div style="float:left;font-size:9px;">'.$row["time"].'</div>';
$output .= '</div>';
$output .= '</div>';
$output .= '</div>';
}
echo $output;
}
<div class="container">
<div class="row">
<div class="col-lg-offset-3 col-lg-6">
<div class="panel">
<div class="panel panel-default" style="border: 1px solid #66512c;">
<div class="panel-heading" style="background-color: #66512c;color: white;">
Market Offers
</div>
<div class="panel-body" style="padding: 0px;padding-left: 5px;border-bottom: 1px solid #66512c;">
<div class="nav nav-pills nav-stacked">
<select name="category" id="user" class="form-control">
<option selected="" value="" class="form-control">All User Offers</option>
<?php echo show_username($connect); ?>
</select>
<div class="panel panel-body" id="show_offer">
<?php echo show_offer($connect);?>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="col-lg-3"></div>
</div>
<script>
$(document).ready(function(){
$('#user').change(function(){
var user_id = $(this).val();
$.ajax({
url:"load_data.php",
method:"POST",
data:{user_id:user_id},
success:function(data){
$('#show_offer').html(data);
}
});
});
});</script>
<?php
//load_data.php
$connect = mysqli_connect("localhost", "root", "", "customer");
$output = '';
if(isset($_POST["user_id"]))
{
if($_POST["user_id"] != '')
{
$query = "SELECT * FROM add_offer WHERE user_id = '".$_POST["user_id"]."' ORDER BY id DESC";
}
else
{
$query = "SELECT * FROM add_offer ORDER BY id DESC";
}
$res = mysqli_query($connect, $query);
while($row = mysqli_fetch_array($res))
{
$output .= '<div class="col-md-3 col-sm-4 col-lg-6">';
$output .= '<div class="panel panel-default">';
$output .= '<div class="panel-body">';
$output .= '<div style="padding:1px;float:left;font-weight:bold;">'.$row["part_no"].'</div>';
$output .= '<div style="padding:1px;float:left;">'.$row["make"].'</div>';
$output .= '<div style="padding:1px;float:left;">'.$row["date_code"].'</div>';
$output .= '<div style="padding:1px;float:left;">'.$row["qty"].'</div>';
$output .= '<br>';
$output .= '<div style="float:left;font-size:9px;">'.$row["time"].'</div>';
$output .= '</div>';
$output .= '</div>';
$output .= '</div>';
}
echo $output;
}?>
In this above code I have used functions to take data from 2 seperate table and I want to display the table in the following format as
name from users table
part_no,make,date_code,qty from add_offers table
the image shows my output market_offers page
I want the data not to be selected by dropdown but has to display whole data as first with name from users table and followed by users part_no from add_offers table.
Also the timestamp display as 2018-03-22 12.40.55 which i want as 22-03-2018 12.40.55
You've to change your code as bellow:
function show_username($connect)
{
$output = '';
$query = "SELECT * from users order by id asc";
$res = mysqli_query($connect, $query);
while($row = mysqli_fetch_array($res))
{
$output .= '<option value="'.$row["id"].'">'.$row["name"].'</option>';
{
}
return $output;
}
function show_offer($connect)
{
$output = '';
$user_query = "SELECT * from users order by id asc";
$user_res = mysqli_query($connect, $user_query );
$user_row = mysqli_fetch_array($user_res);
$query = "SELECT * FROM add_offer where user_id = ".$user_row['id']." ORDER BY id DESC";
$res = mysqli_query($connect, $query);
while($row = mysqli_fetch_array($res))
{
$output .= '<div class="col-md-3 col-sm-4 col-lg-6">';
$output .= '<div class="panel panel-default">';
$output .= '<div class="panel-body">';
$output .= '<div style="padding:1px;float:left;font-weight:bold;">'.$row["part_no"].'</div>';
$output .= '<div style="padding:1px;float:left;">'.$row["make"].'</div>';
$output .= '<div style="padding:1px;float:left;">'.date('d-m-Y H:i:s',strtotime($row["date_code"])).'</div>';
$output .= '<div style="padding:1px;float:left;">'.$row["qty"].'</div>';
$output .= '<br>';
$output .= '<div style="float:left;font-size:9px;">'.$row["time"].'</div>';
$output .= '</div>';
$output .= '</div>';
$output .= '</div>';
}
echo $output;
}
Html Code:
<div class="container">
<div class="row">
<div class="col-lg-offset-3 col-lg-6">
<div class="panel">
<div class="panel panel-default" style="border: 1px solid #66512c;">
<div class="panel-heading" style="background-color: #66512c;color: white;">
Market Offers
</div>
<div class="panel-body" style="padding: 0px;padding-left: 5px;border-bottom: 1px solid #66512c;">
<div class="nav nav-pills nav-stacked">
<select name="category" id="user" class="form-control">
<option selected="" value="" class="form-control">All User Offers</option>
<?php echo show_username($connect); ?>
</select>
<div class="panel panel-body" id="show_offer">
<?php echo show_offer($connect);?>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="col-lg-3"></div>
</div>
Java Script Code:
<script>
$(document).ready(function(){
$('#user').change(function(){
var user_id = $(this).val();
$.ajax({
url:"load_data.php",
method:"POST",
data:{user_id:user_id},
success:function(data){
$('#show_offer').html(data);
}
});
});
});</script>
PHP Code:
<?php
//load_data.php
$connect = mysqli_connect("localhost", "root", "", "customer");
$output = '';
if(isset($_POST["user_id"]))
{
if($_POST["user_id"] != '')
{
$query = "SELECT * FROM add_offer WHERE user_id = '".$_POST["user_id"]."' ORDER BY id DESC";
}
else
{
$query = "SELECT * FROM add_offer ORDER BY id DESC";
}
$res = mysqli_query($connect, $query);
while($row = mysqli_fetch_array($res))
{
$output .= '<div class="col-md-3 col-sm-4 col-lg-6">';
$output .= '<div class="panel panel-default">';
$output .= '<div class="panel-body">';
$output .= '<div style="padding:1px;float:left;font-weight:bold;">'.$row["part_no"].'</div>';
$output .= '<div style="padding:1px;float:left;">'.$row["make"].'</div>';
$output .= '<div style="padding:1px;float:left;">'.$row["date_code"].'</div>';
$output .= '<div style="padding:1px;float:left;">'.$row["qty"].'</div>';
$output .= '<br>';
$output .= '<div style="float:left;font-size:9px;">'.$row["time"].'</div>';
$output .= '</div>';
$output .= '</div>';
$output .= '</div>';
}
echo $output;
}?>

php loop inside accordion not displaying correctly

In my database i have a table called publi inside this table i have two columns pub_year and pub_publi
Example of the table publi content :
<table border="1">
<tr>
<td>2016</td>
<td>content_2016_1</td>
</tr>
<tr>
<td>2016</td>
<td>content_2016_2</td>
</tr>
<tr>
<td>2016</td>
<td>content_2016_3</td>
</tr>
<tr>
<td>2015</td>
<td>content_2015_1</td>
</tr>
<tr>
<td>2015</td>
<td>content_2015_2</td>
</tr>
</table>
and i want this output
<button type="button" class="btn btn-info" data-toggle="collapse" data-target="#2016">2016</button>
<div id="2016" class="collapse">
content_2016_1 <br>
content_2016_2 <br>
content_2016_3 <br>
</div>
<button type="button" class="btn btn-info" data-toggle="collapse" data-target="#2015">2015</button>
<div id="2015" class="collapse">
content_2015_1 <br>
content_2015_2 <br>
</div>
and this is my code :
<?php
$query = "SELECT * FROM publi where pub_type=0 order by pub_year DESC, pub_publi ";
$result = mysqli_query($connection, $query);
$previous =0;
while ($val = mysqli_fetch_array($result))
{
if ($previous <> $val['pub_year'])
{
$previous = $val['pub_year'];
$year = $previous;
echo '<button type="button" class="btn btn-info" data-toggle="collapse" data-target="#';
echo $year;
echo '">';
echo $year;
echo '</button>';
echo '<div id="';
echo $year;
echo '" class="collapse">';
$Temp = highlight("person1",$val['pub_publi'],"0000FF");
$Temp = highlight("person2",$Temp,"0000FF");
$Temp = highlight("person3",$Temp,"0000FF");
$Temp = highlight("person4",$Temp,"0000FF");
$Temp = highlight("person5",$Temp,"0000FF");
$Temp = highlight("person6",$Temp,"0000FF");
$Temp = highlight("person7",$Temp,"0000FF");
$Temp = highlight("person8",$Temp,"0000FF");
$Temp = highlight("person9",$Temp,"0000FF");
$Temp = highlight("person10",$Temp,"0000FF");
echo '<a target=blank href="http://www.test.com/query.f?term=' . $val['pub_pubmed'] . '";)><img border="0" src="img/test.gif" align=MIDDLE alt="Search in for ' . $val['pub_publi'] . '"></a>';
echo $Temp;
echo '</div>';
}
}
?>
but the result that i get is:
<button type="button" class="btn btn-info" data-toggle="collapse" data-target="#2016">2016</button>
<div id="2016" class="collapse">
content_2016_1 <br>
</div>
content_2016_2
content_2016_3
<button type="button" class="btn btn-info" data-toggle="collapse" data-target="#2015">2015</button>
<div id="2015" class="collapse">
content_2015_1 <br>
</div>
content_2015_2 <br>
The way you are grouping the data by pub_year is correct but from your example, you are only outputting information when group is changing.
I would propose something similar:
<?php
$previous = false;
while ($val = mysqli_fetch_array($result)) {
// when group is changing, end previous and start new
if ($previous <> $val['pub_year']) {
// end previous group html markup
if ($previous !== false) {
echo '</div>';
}
$previous = $val['pub_year'];
$year = $previous;
echo '<button type="button" class="btn btn-info" data-toggle="collapse" data-target="#' . $year . '>' . $year . '</button>';
echo '<div id="' . $year . '" class="collapse">';
}
// always output data inside group
$Temp = highlight("person1",$val['pub_publi'],"0000FF");
$Temp = highlight("person2",$Temp,"0000FF");
$Temp = highlight("person3",$Temp,"0000FF");
$Temp = highlight("person4",$Temp,"0000FF");
$Temp = highlight("person5",$Temp,"0000FF");
$Temp = highlight("person6",$Temp,"0000FF");
$Temp = highlight("person7",$Temp,"0000FF");
$Temp = highlight("person8",$Temp,"0000FF");
$Temp = highlight("person9",$Temp,"0000FF");
$Temp = highlight("person10",$Temp,"0000FF");
echo $Temp . '<br>';
}
// end last group html markup
if ($previous !== false) {
echo '</div>';
}
?>

PHP Foreach with two queries

Ok i have one query in codeigniter where i select all data
function get_story()
{
$this->db->select('*');
$this->db->from('stories');
$query = $this->db->get();
return $query;
}
then i make another query with data from another table
function get_stories()
{
$this->db->from('stories');
$this->db->join('stories_to_categories', 'stories.id = stories_to_categories.story');
$this->db->join('categories', 'stories_to_categories.category = categories.id');
$query = $this->db->get();
return $query;
}
Then i am trying to create table with data from two queries. Making one query with another JOIN its not option. I created foreach
foreach($story->result() as $row)
{
echo '<tr>
<td>'.$row->title_english.'</td>
<td>'?????????????????????????????????????????????????????</td>
<td align="center">
<img src="'.site_url('admin/images/icons/pencil.png').'" class="tooltip" title="'.lang('edit').'" />
<img src="'.site_url('admin/images/icons/minus-circle.png').'" class="tooltip" title="'.lang('delete').'" />
</td>
</tr>
';
echo '<div id="delete_'.$row->id.'" style="display: none;">
<h1>'.lang('client_delete').'</h1><br />
'.lang('story_confirm_delete').'
<br /><br />
<span><span>'.lang('yes').'</span></span>
<span><span>'.lang('no').'</span></span>
<br /><br />
</div>';
}
In td tag where is ???? i need results from second query, i have no idea how to insert data from second query into foreach??? Does somebody have idea, helpe is really needed?
If I understand your question, I think you just need to use string concatenation. Prepare your HTML output into a variable and print it later.
$html = '<table>';
foreach($story->result() as $row)
{
$html .= '<tr>';
$html .= '<td>'.$row->title_english.'</td>';
$html .= '<td>';
foreach($stories->result() as $row2){
// display your second query result here
$html .= '?????????????????????????????????????????????????????';
}
$html .= '</td>';
$html .= '<td align="center">';
$html .= '<img src="'.site_url('admin/images/icons/pencil.png').'" class="tooltip" title="'.lang('edit').'" />';
$html .= '<img src="'.site_url('admin/images/icons/minus-circle.png').'" class="tooltip" title="'.lang('delete').'" />';
$html .= '</td>';
$html .= '</tr>';
$html .= '<div id="delete_'.$row->id.'" style="display: none;">';
$html .= '<h1>'.lang('client_delete').'</h1><br />';
$html .= lang('story_confirm_delete');
$html .= '<br /><br />';
$html .= '<span><span>'.lang('yes').'</span></span>';
$html .= '<span><span>'.lang('no').'</span></span>';
$html .= '<br /><br />';
$html .= '</div>';
}
$html .= '</table>';
echo $html;

Categories