I have a very basic and simple script that should display records from my database. The problem: it doesn't show all the records. I've tried it even with the most simple mysql
($sql="SELECT * FROM $tbl_name";) but still some records are missing (mostly the first of the list that isn't shown).
So here is my code (it's all on 1 page):
<?php
$host="localhost";
$username="***";
$password="***";
$db_name="***";
$tbl_name="***";
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$sql="SELECT * FROM $tbl_name WHERE rowNameOne >= 0.01 AND rowNameTwo='2013'";
if ($_GET['sort'] == 'one')
{
$sql .= " ORDER BY one ASC";
}
elseif ($_GET['sort'] == 'two')
{
$sql .= " ORDER BY two ASC";
}
elseif ($_GET['sort'] == 'three')
{
$sql .= " ORDER BY three ASC";
}
elseif($_GET['sort'] == 'four')
{
$sql .= " ORDER BY four ASC";
}
elseif($_GET['sort'] == 'five')
{
$sql .= " ORDER BY five ASC";
}
$result=mysql_query($sql);
$rows=mysql_fetch_array($result);
?>
<body onload="parent.alertsize(document.body.scrollHeight);">
<br />
<table cellspacing="0" cellpadding="0" align="center">
<tr>
<td valign="top" colspan="5">
<font>Titel</font>
</td>
<tr>
<td>Titel one</td>
<td>Titel two</td>
<td>Titel three</td>
<td>Titel four</td>
<td>Titel five</td>
</tr>
<tr>
<td colspan="5" class="noBorder">
<?php
while($rows=mysql_fetch_array($result)){
?>
<a href="pageName.php?id=<? echo $rows['id']; ?>" >
<table width="100%">
<tr>
<td><? echo $rows['rowNameOne']; ?></td>
<td><? echo $rows['rowNameTwo']; ?></td>
<td><? echo $rows['rowNameThree']; ?></td>
<td><? echo $rows['rowNameFour']; ?></td>
<td><? echo $rows['rowNameFive']; ?></td>
</tr>
</table>
<input name="id" type="hidden" id="id" value="<? echo $rows['id']; ?>">
<?php
}
?>
</a>
</td>
</tr>
</table>
It's a very basic code, easy as can be I would say, but still it's missing records, not displaying everything that's in the database. What am I doing wrong?
Thanks for the help!
Before you start the loop, you do this:
$rows=mysql_fetch_array($result);
Then the loop condition is:
while($rows=mysql_fetch_array($result)){
So the first result is never shown. I would advice to remove the first statement, since you're not using its results between that statement and the loop.
On a related note, please consider moving to PDO or mysqli.
Related
I have some data in mysql database, some of them are have same value.
I already show it in my program:
But it is possible to make rowspan like this?
this is my code.
<tbody class="table-bordered">
<?php $i = 1;
foreach ($altrankq1 as $alt) : ?>
<tr>
<td class="text-center"><?= $i; ?></td>
<td class="text-center"><?= $alt['kode_alternatif']; ?></td>
<td class="text-center"><?= round(($alt['nilai_Q1']), 4); ?></td>
</tr>
<?php $i++;
endforeach; ?>
</tbody>
maybe u can check the 'nilai_q1' like this(?)
<tbody class="table-bordered">
<?php $i = 1; $a = null;
foreach ($altrankq1 as $alt) : ?>
<tr>
<td class="text-center">
<?php
$b = round(($alt['nilai_Q1']), 4);
if($a === $b) {
$i = $i - 1;
echo $i;
} else {
echo $i;
}
$a = $b;
?>
</td>
<td class="text-center"><?= $alt['kode_alternatif']; ?></td>
<td class="text-center"><?= round(($alt['nilai_Q1']), 4); ?></td>
</tr>
<?php $i++;
endforeach; ?>
</tbody>
nb: not tested yet and isnt rowspan
or trying with jquery, i found this answer in stackoverflow rowspan-table-if-values-is-the-same-in-php
How can I use Distinct with join in CodeIgniter, I'm trying to fetch customer's name and registered category, but my problem is whiles fetching the data I'm getting data in repetation because of sub-category, I've used distinct but then I'm getting Ambiguous field error, how can I solve this problem.
My View
<table class="table table-hover example1">
<thead>
<tr>
<th>SNo.</th>
<th>Business Name</th>
<th>Category Name</th>
<th>Edit</th>
</tr>
</thead>
<tbody>
<?php $i=0;
foreach($value as $row){ ?>
<tr>
<td><?php echo ++$i;?></td>
<td><?php echo $row->Bussiness_Name;?></td>
<td><?php echo $row->Category_Name;?></td>
<td onClick="RestoreVendor(<?php echo $row->Business_Id; ?>,<?php echo $row->Trash;?>,'<?php echo $row->Bussiness_Name;?>')" class="btn btn-primary">Remove</td>
</tr>
<?php }?>
</tbody>
</table>
My Controller:
public function removecategory()
{
$this->load->model('VendorModel');
$data = $this->VendorModel->remove_category();
$this->load->view('admin/remove_cat', array('value'=>$data));
}
My Model
public function remove_category()
{
$this->db->select('*');
$this->db->distinct('Category_Id');
$this->db->from('business_mapping');
$this->db->join('business_profile_details', 'business_profile_details.Business_Id = business_mapping.Business_Id');
$this->db->join('category_master', 'category_master.Category_Id = business_mapping.Category_Id');
$query = $this->db->get();
return $query->result();
}
result image
You can use below mentioned query.
$query = $this->db->group_by('category_master.Category_Id,business_profile_details.Business_Id');
Please try above, It will help you.
you can use
$this->db->distinct();
Some times distinct not works then you can use
$this->db->group_by('business_mapping.unique_coloumn_Name');
Trying to add borders and column headings to a table of results from an SQL query.
The results display fine however there are no headings or borders on the table, so it just looks like a jumble of results.
</div>
<div id="section">
<h2>Upcoming Events</h2>
<?php
$connection = mysqli_connect('localhost', 'c3437691', 'chelsea27', 'c3437691');
?>
<?php
$query = "SELECT * FROM Event";
$result=mysqli_query($connection, $query);
echo "<table>";
while ($row=mysqli_fetch_assoc($result)){
?>
<tr>
<td><?php echo $row['Event_Name']?></td>
<td><?php echo $row['Event_Location']?></td>
<td><?php echo $row['Event_Date']?></td>
<td><?php echo $row['Ticket_Price']?></td>
<td><?php echo $row['Ticket_Stock']?></td>
</tr>
<?php
}
?>
</div>
You need to explicitly output your table headers -- simply iterating over results from mysql will not include column names.
After your opening <table> but before your while loop, add the following:
<tr>
<th>Event Name</th>
<th>Event Location</th>
<th>Event Date</th>
<th>Ticket Price</th>
<th>Ticket Stock</th>
</tr>
If you want to add borders to the table, you should apply CSS to the table.
Help me!!
Seem when i run the scripts, the output seems wrong :(
This is the output...
Here My Output
The 1st and 3rd entry is true, online
But the rest should be OFFLINE but the output shows all ONLINE
I think the problem is from foreach loop but I cannot figure out how to solve the problem
Please help me... :(
<table border="1">
<thead>
<tr>
<th>Entry</th>
<th>Host</th>
<th>Name</th>
<th>Location</th>
<th>Description</th>
<th>Availability</th>
</tr>
</thead>
<tbody>
<?php
$sql = "SELECT * FROM host_entry";
$no = 1;
//foreach($dbh->fetchAll(PDO::FETCH_ASSOC) as $row)
$dbhi = $dbh->query($sql);
foreach ($dbhi->fetchAll(PDO::FETCH_BOTH) as $data) {
?>
<tr>
<td><?php echo $no++; ?></td>
<td><?php echo $data['host_server']; ?></td>
<td><?php echo $data['host_name']; ?></td>
<td><?php echo $data['host_location']; ?></td>
<td><?php echo $data['host_desc']; ?></td>
<td>
<?php
$ip = $data["host_server"];
// Run the ping to the IP
exec ("ping -n 1 -w 1 $ip", $ping_output);
if(preg_match("/Reply/i", $ping_output[2])!==0) {
echo "<font color='green'><strong>Online!</strong></font>";
}
else if(preg_match("/Reply/i", $ping_output[2])===0){
echo "<font color='red'><strong>Offline!</strong></font>";
}
?>
</td>
</tr>
<?php
}
?>
</tbody>
This is supposed to be a simple high scores table. but everytime it pulls another player from the database it looks like this.
(table headers)
player
(table headers)
player
(table headers)
player
and so on you get the picture... I want it to just show the header one time at the top of the list of players... I had it working at one time and don't remember what I changed that caused it to do this, does anyone know how to fix it?
Here is my code:
<?php
$result = mysql_query("SELECT * FROM users ORDER BY explores DESC LIMIT 20;")
or die(mysql_error());
while($row = mysql_fetch_array( $result )) {
?>
<table class="reg-box" style="width: 600px; text-align: center;" align="center" cellspacing="5">
<tbody>
<tr>
<th colspan="2">Trainer</th>
<th colspan="2">Explores</th>
</tr>
<tr>
<td colspan="2"><?php echo "".$row['username'].""; ?></td>
<td colspan="2"> <?php echo $row['explores']; ?></td>
</tr>
</tbody>
</table>
<?php } ?>
The problem with your code was the table is getting created in the loop, leading to the multiple headers. Instead remove the creation of the table from the while loop and then try to update all the result set you get from the query you fired in a loop. Try this.
$result = mysql_query("SELECT * FROM users ORDER BY explores DESC LIMIT 20;")
or die(mysql_error());
?>
<table class="reg-box" style="width: 600px; text-align: center;" align="center" cellspacing="5">
<tbody>
<tr>
<th colspan="2">Trainer</th>
<th colspan="2">Explores</th>
</tr>
<?php
while($row = mysql_fetch_array( $result )) {
<tr>
<td colspan="2"><?php echo "".$row['username'].""; ?></td>
<td colspan="2"> <?php echo $row['explores']; ?></td>
</tr>
<?php } ?>
</tbody>
</table>