Table doesn't align vertically - php

So I want to do something very simple, which I can't figure out properly. I'm currently making a forum and where I normally have errors (the back end) it all works now, probably through the black magic I used ;D,
So now, the thing is: I want to show all my subcategories, so I'm using the following table:
<div id="content_big">
<div class="content_top_oranje">
<p class="koptekst">
<?php echo 'Name';?>
</p>
<p style="float:left; margin-left:70%; margin-top:-30px; color:#fff; font-size:11px;font-family:Ubuntu;">
<?php echo 'Description';?>
</p>
</div>
<div class="content_mid">
<?php $kop='algemeen' ;
$query=$ db->conn->prepare("SELECT * FROM ht_forum_categorien WHERE kop = ?");
$query->bind_param('s', $kop);
$query->execute();
$result = $query->get_result();
while ($row = $result->fetch_assoc()) { ?>
<table style="width:100%;" border="1" cellspacing="0">
<tr>
<td>
<?php echo $row[ 'name']; ?>
</td>
<td style="margin-left:50px;">
<?php echo $row[ 'description]; ?>
</td>
</tr>
</table>
<?php
}
?>
</div>
</div>
But when I use that, this is the output I get:
How is it possible that all of the rows won't vertically align? And how to solve it?
Thanks. (I'm sorry the screenshot is in dutch, but I can't change the values in the DB, since it isn't mine

You have two fairly big problems in your code:
most importantly, and the problem you ran into: you're not generating a table. You're generating lots of tables. If you want one table, move your <table> and </table> tags outside of your loop and only generate <tr> with their content in your while loop.
a problem you didn't realise you had: the HTML you're using isn't faithful to the promise that your markup is semantic: that heading should be a heading element (h1, h2, h3, what have you), and as a table, really this shouldn't be divs and p and table at all, this should be a single table, with a heading row, styled as your heading, and then data rows.
So let's rewrite this to something that makes sense both from a PHP and from an HTML perspective:
<?php
...query your data. Do this first...
$result = ...;
if($result isn't empty) {
?>
<table>
<?php
echo "<thead><th>$kop</th><th>$description</th></thead>";
?>
<tbody>
<?php
while ($row = $result->fetch_assoc()) {
$name = $row['name'];
$desc = $row['description'];
echo "<tr><td>$name</td><td>$desc</td></tr>\n";
}
?>
</tbody>
</table>
<?php } ?>
So don't do the querying inside the table, do it first, then if there are results to show, decide whether or not to write the single table, generating the dynamic content in the tbody as table rows. And use CSS to style the thead cells the way you need.
For that matter, don't use all those inline style="..." bits. They're all identical, so you're just needlessly repeating static CSS. Use class="rowclass" or whatever name you think is appropriate, and then in your stylesheet or <style> block, define
.rowclass {
background-color: ...;
color: ...;
...
}

Try this
<div id="content_big">
<div class="content_top_oranje">
<p class="koptekst">
<?php echo 'Name';?>
</p>
<p style="float:left; margin-left:70%; margin-top:-30px; color:#fff; font-size:11px;font-family:Ubuntu;">
<?php echo 'Description';?>
</p>
</div>
<div class="content_mid">
<?php $kop='algemeen' ;
$query=$ db->conn->prepare("SELECT * FROM ht_forum_categorien WHERE kop = ?");
$query->bind_param('s', $kop);
$query->execute();
$result = $query->get_result();
?>
<table style="width:100%;" border="1" cellspacing="0">
<?php while ($row = $result->fetch_assoc()) { ?>
<tr>
<td>
<?php echo $row[ 'name']; ?>
</td>
<td style="margin-left:50px;">
<?php echo $row[ 'description']; ?>
</td>
</tr>
<?php
}
?>
</table>
</div>
</div>

Related

Aligning image from php using table

Im trying to align the image that I fetched from my database with a text that is also fetched from database. The text seems ok but the image just stick to the left
<div align="center">
<p>
<?php
$vid =$_REQUEST[#id];
include 'conn.php';
$sql = mysql_query("SELECT * FROM product_car where Id = '$vid'");
$vid = 'Id';
$vnamaproduk = 'NamaProduk';
$vharga = 'Harga';
$vpenerangan = 'Penerangan';
$vgambar = 'Gambar';
?>
<table width="1000" border="0" align="center">
<?php
while($row= mysql_fetch_assoc($sql)){
?>
<tr>
<td>
<img src="gambar/car/<?php echo $row[$vgambar];?>"width="500" height="400"/>
</td>
<td>
<?php
echo "<br>Product Id : ".$row[$vid];
echo "<br>Product Name : ".$row[$vnamaproduk];
$harganew =sprintf('%0.2f',$row[$vharga]);
echo "<br>Price : RM".$harganew;
echo "<br> <br>".$row[$vpenerangan];
echo "<br>";
}
?>
ADD TO CART
</td>
</tr>
</table>
</div>
is the image displayed as a block?
img {
display:block;
margin:auto;
}
If you want to center the image, you could try this
img {
margin: auto 0;
}

Foreach loop are displaying same rows multiple times

I have a problem with my first attempt at making a foreach loop.
My problem is, that I'm only trying to call out two rows to be displayed, which kinda works, although they are being displayed as many times as there are different rows in my table.
My code looks like this:
$sql = "SELECT * FROM webpages";
$result = mysql_query($sql);
$assoc_query = mysql_fetch_assoc($result);
<?php foreach ($assoc_query as $value) { ?>
<tr>
<td>
<div id='pageimg'><img src= <?php echo $assoc_query['pic'];?> ></div>
</td>
<td>
<div id="pagename"><?php echo $assoc_query['name']; ?> </div>
</td>
</tr>
<?php } ?>
It's being displayed like so on the page:
/picture/ DAK
/picture/ DAK
/picture/ DAK
/picture/ DAK
Hope you can help me:)
<?php
$sql = "SELECT * FROM webpages";
$result = mysql_query($sql);
$counter=0;
while($row= mysql_fetch_assoc($result))
{
$assoc_query[$counter] = $row;
$counter++;
}
foreach ($assoc_query as $value) { ?>
<tr>
<td>
<div id='pageimg'><img src= <?php echo $value['pic'];?> ></div>
</td>
<td>
<div id="pagename"><?php echo $value['name']; ?> </div>
</td>
</tr>
<?php } ?>
Instead of foreach you will want a while. What you are doing here is looping the elements of array $assoc_query.
A while loop will get the next result set to be used.
while($assoc_query = mysql_fetch_assoc($result)){
//Do your thing
}
Also please consider updating from mysql_ to mysqli_ or PDO. What you are using is depreciated and there is added security with the newer ones.
this can't be done with foreach loop unless you gonna show 1 user's information only using limit Clause or Where Clause otherwise that it's useless
instead you need a while loop like this
<?php while ($assoc_query = mysql_fetch_assoc($result)) { ?>
<tr>
<td>
<div id='pageimg'><img src= <?php echo $assoc_query['pic'];?> ></div>
</td>
<td>
<div id="pagename"><?php echo $assoc_query['name']; ?> </div>
</td>
</tr>
<?php } ?>
You should do it like this:
$sql = "SELECT * FROM webpages";
$result = mysql_query($sql);
while ($assoc_query = mysql_fetch_assoc($result)) {
?>
<tr>
<td>
<div id='pageimg'><img src= <?php echo $assoc_query['pic'];?> ></div>
</td>
<td>
<div id="pagename"><?php echo $assoc_query['name']; ?> </div>
</td>
</tr>
<?php } ?>
That should work. But as said before you shouldn't code like this. Use mysqli or PDO like nerdlyist says.
#Nerdlyist
$sql = "SELECT * FROM webpages";
$result = mysql_query($sql);
$assoc_query = mysql_fetch_assoc($result);
<?php while ($assoc_query = mysql_fetch_assoc($result)) {
?>
<tr>
<td>
<div id='pageimg'><img src= <?php echo $assoc_query['pic'];?> ></div>
</td>
<td>
<div id="pagename"><?php echo $assoc_query['name']; ?> </div>
</td>
</tr>
<?php } ?>

Loop through database fields PHP MySQL

I am working on an election system and I am having difficulty solving a problem. Following is how my application looks like.
At the moment I get these results by adding the query multiple times on my php file. (for example run it where RaceID = 9, RaceID = 10 .........)
I am sure there should be a way of reading the RaceID as an array or some other way and get the results that way. Since these are JOIN tables I am also looking for a way of retrieving the RaceName (Presidential Names, Justice Supreme Court ... etc) and MainRaceName(Titles with red, blue, gray) as well. I hope I am making some sense so far...
Following is my code for
<!-- MAIN ELECTION TICKET MainID loop should control this -->
<div class="panel panel-red margin-bottom-40">
<div class="panel-heading">
<h2 class="panel-title"> <strong>REPUBLICAN NATIONAL</strong> </h2>
</div>
<!-- End then SUB ELECTION TICKET RaceID loop should control this section-->
<h3 style="background-color:#f5f5f5; margin:30px; padding:5px; font-weight:Bold ">Presidential Race </h3>
<!-- Finally Results section ELECTION RESULTS -->
<table class="table table-hover">
<thead>
<tr>
<th></th>
<th>Candidate</th>
<th>Votes</th>
<th>%</th>
</tr>
<!-- This area gets the sum of the votes for a specific RaceID -->
<?php
$result = mysql_query('SELECT SUM(CandidateVotes) AS value_sum FROM candidates WHERE RaceID =9');
$row = mysql_fetch_assoc($result);
$sum = $row['value_sum'];
?>
</thead>
<tbody>
<?php
$query = "SELECT candidates.CandidateName, candidates.CandidateVotes, candidates.Party, mainrace.MainRaceName, race.RaceName, candidates.win
FROM candidates
JOIN race ON race.RaceID = candidates.RaceID
JOIN mainrace ON mainrace.MainID = candidates.MainID
WHERE candidates.RaceID = 9";
$result = mysql_query($query) or die(mysql_error());
for($i=0; $row = mysql_fetch_array($result); $i++){
?>
<tr>
<!--Show winner Icon if the win field is selected as 1 from database -->
<td width="5px">
<?php if ($row['win']==1)
{
echo "<span class=\"glyphicon glyphicon-check\"></span>";
}
else
{
echo "<span class=\"glyphicon glyphicon-unchecked\" style=\"color:#cccccc\"></span>";
}
?>
</td>
<td><?php if ($row['win']==1){
echo "<strong>";
echo $row['CandidateName'];
echo "</strong>";
}
else {
echo $row['CandidateName'];
}
?>
</td>
<td width="20%"><?php echo $row['CandidateVotes']; ?></td>
<td width="30%"><?php
if ($row['CandidateVotes']>0){
echo number_format((float)(($row['CandidateVotes']/$sum)*100), 2, '.', ''). ' %';
}
else{
echo "N/A";
}
?>
</td>
</tr>
<?php
}
?>
<tr>
<td></td>
<td><strong>Total Votes:</strong></td>
<td><strong><?php echo $sum ?></strong></td>
<td></td>
</tr>
</tbody>
</table>
I really appreciate if you can provide some samples (loops) how I can resolve this problem. Your help is much appreciated. Sincerely,
You should be able to pull down everything you need in one query.
In order to sum up votes by candidate, you need to make sure you GROUP BY the candidate name.
Try something like this:
SELECT
SUM(candidates.CandidateVotes) AS value_sum,
candidates.CandidateName,
candidates.Party,
mainrace.MainRaceName,
race.RaceName,
candidates.win
FROM candidates
JOIN race
ON race.RaceID = candidates.RaceID
JOIN mainrace
ON mainrace.MainID = candidates.MainID
WHERE candidates.RaceID = :raceId
GROUP BY candidates.CandidateName

The word "(Array)" is printing in my table instead of the actual objects array

All the code is within the same index file and same class. I can't seem to figure out why its not calling. Both objects themselves work in a call 2-3 lines right above, outside this table.... I tried calling them inside the table to no avail. They just pop up the word '(Array)' in both boxes. Is their a syntactical thing i am missing? On my website I print the table, with the missing functions inside table, and the two tables that are supposed to be inside the table printed right above and below the table. For reference, my website for the school project: http://www.wallofkron.x10host.com
<?php
$companyobject = new BurgerJoint(); //object created
$companyobject->getLeftNavBar($companyobject->navbar_array); //call to leftnav bar works
$companyobject->displayProduct($burgerarray); // burger table also works outside of table
print "<TABLE style='width:100%'height=200 BORDER='1'>
<TR><TD style='width:15%'>$companyobject->getLeftNavBar($companyobject->navbar_array)
</TD>
<TD>$companyobject->displayProduct($burgerarray)
</TD></TR>
</TABLE>";
function displayProduct($array){
print "<TABLE BORDER = '1'>";
foreach ($array as $oneitem) {
print "<TR><TD>$oneitem</TD></TR>";
} print "</TABLE>";
}
function getLeftNavBar($array){
print "<TABLE BORDER = '1'>";
foreach($array as $key => $value) {
print "<TR><TD><A HREF=$value>$key</A></TD></TR>";
}
print "</TABLE>";
}
?>
i think i noticed the problem:
<?php
print "<TABLE style='width:100%'height=200 BORDER='1'>
<TR><TD style='width:15%'>$companyobject->getLeftNavBar($companyobject->navbar_array)
</TD>
<TD>$companyobject->displayProduct($companyobject->burger_array)
</TD></TR>
</TABLE>";
?>
i think that the problem was that you were not reffering to the actual burgerarray inside the object companyobject.
as told in the comment it would look something like this:
<!DOCTYPE html>
<html>
<head>
<title>Bob's Burgers</title>
</head>
<body>
<header style="background:orange; width:100%;">
<div style="text-align:center;">
<?php foreach($companyobject->navbar_array as $nav){
echo $nav
}?>
</header>
<main style="width:100%;">
<table style="height:200px; border:1px solid black;">
<?php foreach($companyobject->burger_array as $burger):?>
<tr><td><?php echo $burger; ?></td></tr>
<? endforeach; ?>
</table>
</main>
</html>

Table inside a loop

I have one table in loop which come under li:
<?php
for($i=1;$i<=$tc;$i++)
{
$row=mysql_fetch_array($result);
?>
<li style="list-style:none; margin-left:-20px">
<table width="600" border="0" cellspacing="0" cellpadding="0">
<tr>
<td class="hline" style="width:267px"><?php echo $row['tit'] .",". $row['name'] ?></td>
<td class="vline" style="width:1px"> </td>
<td class="hline" style="width:100px"><?php echo $row['city']; ?></td>
</tr>
</table>
</li>
<?php
}
?>
The output comes like this:
alt text http://img20.imageshack.us/img20/4153/67396040.gif
I can't put table outside the loop, due to <li> sorting
if you can't use table outside the loop then i think best option will be use of
<div>
statement
for example
<div class="q-s na">
<div class="st" style="margin-right:10px; width:150px">
<div class="m-c"><?php echo $row['tit'] .",". $row['name'] ?></div>
</div>
this will be same as you one
<td>
you can define style according to your requirements.
for example
<style>
.q-s{overflow:hidden;width:664px;float:left;
padding-top:2px; padding-bottom:2px; height:25px}
.na .st{ float:left;}
.na .m-c {border-bottom:1px dotted #999; padding-bottom:10px; height:15px}
</style>
i can't put table outside the loop.
Why not? This is where it belongs. After all, you (logically) produce one table, not many of them. No need for the list item, either.
If you're unable to put the table outside of the loop, you should probably just use tags rather than creating tables, as you're defeating the purpose of even having a table by making each table a single row and trying to stack them.
Another thing to note if you are going to stick with tables:
If you're hard-coding the table width AND all of the table cell (column) widths, it may cause unexpected issues when they don't add up:
Table width = 600px
Cells = 267 + 1 + 100 = 368px

Categories