echo doesn't work correctly - php

Echo works fine at other lines But when i try to add tag to this table, i see that tags placed out of the tag.
<table>
<th style="cursor:pointer;border-radius:5px 0px 0px 0px;">Başlık</th>
<th style="cursor:pointer;">Başlatan</th>
<th style="cursor:pointer;border-radius:0px 5px 0px 0px;">Tarih</th>
$sonuc = mysql_query("select A.subject, A.poster_name, A.poster_time, A.id_msg, A.id_topic, B.id_first_msg, B.id_member_started from smf_messages A, smf_topics B WHERE A.id_msg = B.id_first_msg ORDER BY id_topic DESC LIMIT 10");
if(mysql_num_rows($sonuc)!=0)
{
while($oku = mysql_fetch_object($sonuc))
{
echo '<tr id="iceriktablo" style="cursor:pointer;margin-top:0;margin-bottom:0;">';
echo '<a href="forum/index.php?topic='. $oku->id_topic .'"><td style="font-size:13px;font-weight:bold;">';
echo $oku->subject;
echo '</td></a>';
echo '<a href="forum/index.php?topic='. $oku->id_topic .'"><td style="font-size:13px;font-weight:bold;"><center><b>';
echo $oku->poster_name;
echo '</b></center></td></a>';
echo '<td style="font-size:13px;font-weight:bold;"><center><b>';
$zaman = $oku->poster_time;
echo gmdate("d-m-Y\ H:i:s", $zaman);
echo '</b></center></td></tr></a>';
}
}else{
echo "Hiçbir kayıt bulunamadı!";
}
mysql_close($conn);
?>
</table>
result as inspect element:
http://puu.sh/qed4c/7b04611347.png
result:
enter image description here
filename: index.php

Your HTML is invalid:
echo '<a href="forum/index.php?topic='. $oku->id_topic .'"><td style="font-size:13px;font-weight:bold;"><center><b>';
echo $oku->poster_name;
echo '</b></center></td></a>';
The A-tag isn't allowed to wrap TD (and of course some more). I guess the browser corrected that automatically.

Tag A is not a part of a table to it will be always placed out of it. You can't do somethig like this
<table>
<tr>
<a><td></td></a>
</tr>
</table>
You should place your A tag inside TD or TH tag.

Related

How can I edit the rows of data in a table using data from a database?

How can I format and style the rows of data in this table where it's displaying my data?
<table class="table">
<tr bgcolor=" #b366ff">
<th>Game</th>
<th>Date</th>
<th>Score</th>
<th>Venue</th>
</tr>
<?php
$sql = "SELECT * FROM game WHERE username = '{$users}' AND savename = '{$saves}';";
$result = mysqli_query($connection, $sql);
?>
<?php
while ($row = mysqli_fetch_assoc($result)){
echo "<tr>";
echo "<td>".$row['team']."</td>";
echo "<td>".$row['date']."</td>";
echo "<td>".$row['score']."</td>";
echo "<td>".$row['venue']."</td>";
echo "</tr>";
}
?>
using a CSS class?
<style>
.tr1{background-color:#333;}
.td1{background-color:#999;}
</style>
echo "<tr class="tr1">";
echo "<td class="td1">".$row['team']."</td>";
You can do same as you do in normal HTML, you can use
.table tr{
background-color: grey;
}
Also you can use nth child selecter to style the elements.

How do i put a <td> in my <table> with php?

I’m working on a php snippet and i made a table. I tried putting a td tag inside but when i do this, a lot disappears. This is a piece of my code:
//Use the functions of the client, the params of the function are in
//the associative array
$params = array('customerid' => '1532');
$response = $soapclient->ca_customer_products($params);
echo '<table><tbody><tr><th>Product</th><th>Naam</th> <th>Prijs</th><th>Qte</th></tr>';
echo '<table style="border-style: solid; border-width:1px;">';
foreach($response->list->element as $product) {
if($product->stock > 0) {
echo '<tr>';
echo '<td style="display: flex; border: 1px solid black;">';
//echo '<td>';
echo '<img src="' . $product->url . '" class="php_image" style="width: 15%; height: 15%;"/>';
//echo '<img style="width: 15%;">';
//echo '</td>';
print_r($product->description);
echo "<p style='color:green;'>".$product->price1."</p>";
echo "<p style='color:red; text-decoration: line-through'>".$product->price2."</p>";
print_r($product->price1);
print_r($product->price2);
print_r($product->stock);
echo '</tr>';
}
}
echo '</tbody></table>';
The code behind the // is where i tried to put the td tag but when i put it there, the images that normally appear go blank and when i inspect my code there is a lot of other code that also disappears. What am i doing wrong here?
Thank you for your help!
First I can see a problem with these lines :
echo '<table><tbody><tr><th>Product</th><th>Naam</th> <th>Prijs</th><th>Qte</th></tr>';
echo '<table style="border-style: solid; border-width:1px;">';
Because you just close your first TABLE at the end, but not the other inside :
echo '</tbody></table>';
There are some inconsistency of your <td> to <th>. You can have a look below
$params = array('customerid' => '1532');
$response = $soapclient->ca_customer_products($params);
echo '<table style="border-style: solid; border-width:1px;">
<thead>
<tr>
<th>Product</th>
<th>Naam</th>
<th>Prijs</th>
<th>Qte</th>
</tr>
</thead><tbody>';
foreach($response->list->element as $product) {
if($product->stock > 0) {
echo "<tr>
<td style='display: flex; border: 1px solid black;'>
<img src='$product->url' class='php_image' style='width: 15%; height: 15%;'/>
</td>
<td>Your product name</td>
<td>$product->description</td>
<td>
<p style='color: green;'>$product->price1</p>
<p style='color: red;'>$product->price</p>
</td>
<td>$product->stock</td>
</tr>";
}
}
echo '</tbody></table>';
?>
There's a lot problems with the code. From what i can see in your code, you have a table inside a tbody and at the end closed only one table.
Secondly you're also trying to put a td inside another td which is not the right thing to do. Check out mozilla developer website for more information on using HTML tables.
//Use the functions of the client, the params of the function are in
//the associative array
$params = array('customerid' => '1532');
$response = $soapclient->ca_customer_products($params);
echo '<table style="border-style: solid; border-width:1px;"><tbody><tr><th>Product</th><th>Naam</th> <th>Prijs</th><th>Qte</th></tr>';
foreach($response->list->element as $product) {
if($product->stock > 0) {
echo '<tr>';
echo '<td style="display: flex; border: 1px solid black;">';
//echo '<td>';
echo '<img src="' . $product->url . '" class="php_image" style="width: 15%; height: 15%;"/>';
//echo '<img style="width: 15%;">';
//echo '</td>';
print_r($product->description);
echo "<p style='color:green;'>".$product->price1."</p>";
echo "<p style='color:red; text-decoration: line-through'>".$product->price2."</p>";
print_r($product->price1);
print_r($product->price2);
print_r($product->stock);
echo '</td></tr>';
}
}
echo '</tbody></table>';
You are not closing your tags correctly. Also checkout the docs just as #christopher_bincom has mentioned.

What is the best way to change row colors in PHP MySQL dynamic table?

I insert MySQL db result into HTML table, after that I am trying to change row colors using two colors,
for example if 1st row has red 2nd row has yellow again 3rd row has
red like wise..
what is the best possible way to do that, I wrote PHP code using PHP modulus function, is there any easist way to do that thank you..
<?php
$result = mysqli_query($link, "SELECT * FROM example");
?>
<table>
<tr>
<th>Name</th>
<th>Age</th>
</tr>
<?php
$i = 0;
while ($row = mysqli_fetch_assoc($result)) {
if ($i % 2 == 0) {
$bgColor = ' style="background-color:#CCFFFF;" ';
} else {
$bgColor = ' style="background-color:#FFFF99;" ';
}
echo "<tr>";
echo "<td $bgColor>";
echo $row['name'];
echo "</td>";
echo "<td $bgColor>";
echo $row['age'];
echo "</td>";
echo "</tr>";
$i++;
}
?>
</table>
It can be done via CSS
tr:nth-child(even) {background: yellow}
tr:nth-child(odd) {background: red}
Source: http://www.w3.org/Style/Examples/007/evenodd.en.html
include css
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
replace table tag with
<table class="table table-striped" >
Instead of inline styles you should use classes .red and .yellow.
<?php
$result = mysqli_query($link, 'SELECT * FROM example');
?>
<table>
<tr>
<th>Name</th>
<th>Age</th>
</tr>
<?php
$i = 0;
while ($row = mysqli_fetch_assoc($result)) {
echo '<tr class="' . (($i % 2 == 0) ? 'red' : 'yellow') . '">';
echo '<td>';
echo $row['name'];
echo '</td>';
echo '<td>';
echo $row['age'];
echo '</td>';
echo '</tr>';
$i++;
}
?>
</table>
You able to make it not using PHP at all.
Just use CSS with :nth-child pseudo-class
tr:nth-child(2n) {
background: #f0f0f0; /* row background color */
}
tr:nth-child(1) {
background: #666; /* row background color */
color: #fff; /* text color */
}
By the way, It's very bad way to show data in interface to mixing process of getting it from DB and showing it in view (putting to output buffer). This code looks like as an traning example fragment from old ugly bloody book for PHP3. It's reqired to separate 2 process: first getting ALL data from DB, and after that puting it into output with appearance.

Adding a hyperlink into a PHP search script that is echo'ing results

I have the below search script, and I need to have the COMPANY section of the results link to each companies website (stored in column WEBSITE in my database). Can anyone help?
if(isset($_POST['completedsearch'])) {
$term = $_POST['query'];
$mysql = mysql_connect("ldb504.securepod.com","martindb","green11");
mysql_select_db("hcsd");
$qu = mysql_query("SELECT * FROM Sheet1 WHERE COMPANY LIKE '%".mysql_real_escape_string($term)."%' OR LOCATION LIKE '%".mysql_real_escape_string($term)."%' OR KEYWORDS LIKE '%".mysql_real_escape_string($term)."%' OR PRODUCTSSERVICES LIKE '%".mysql_real_escape_string($term)."%' "); //selects the row that contains ANYTHING like the submitted string
echo "Searching for '$term'";
echo "
<table><tr style=\"border-bottom:1px dotted #305896;\"><th style=\"padding:0px 10px;text-align:left;border-bottom:1px dotted #305896;\">Company</th>
<th style=\"padding:0px 10px;text-align:left;border-bottom:1px dotted #305896;\">Location</th>
<th style=\"padding:0px 10px;text-align:left;border-bottom:1px dotted #305896;\">Products/Services</th></tr>";
while($row = mysql_fetch_array($qu)) {
echo"<tr style=\"border-bottom:1px dotted #305896;\"><td style=\"padding:0px 10px;text-align:left;border-bottom:1px dotted #305896;\">";
echo"'.htmlspecialchars($row['COMPANY']).'";
echo "</td>";
echo "<td style=\"padding:0px 10px;text-align:left;border-bottom:1px dotted #305896;\">";
echo htmlspecialchars($row['LOCATION']);
echo "</td>";
echo "<td style=\"padding:0px 10px;text-align:left;border-bottom:1px dotted #305896;\">";
echo htmlspecialchars($row['PRODUCTSSERVICES']);
echo "</td></tr>";
}
echo "</table>";
}
You have an error in your php mixing the single and double quotes:
echo"'.htmlspecialchars($row['COMPANY']).'";
Should be something like:
echo ''.htmlspecialchars($row['COMPANY']).'';

DIV element not being pushed down by content inside it

I have a div which is not being pushed down by the content inside it. Instead the content just overlaps the div. I assume this is because there's a PHP while loop between the div tags? How do I fix this?
session_start();
if (!$_SESSION["user_name"])
{
header("Location: index.php");
}
include('header.php');
$id = $_GET['id'];
if(isset($id)) {
connect_to_db();
mysql_query("DELETE FROM content WHERE id='$id'");
$deleted = 'Content Successfully Deleted.<br>';
}
echo '<div id="content">';
echo '<h2>Delete Content</h2>';
if(isset($deleted)){
echo $deleted;
}
connect_to_db();
$query="SELECT id, date, title, image FROM content ORDER BY date DESC";
$result=mysql_query($query);
while($row = mysql_fetch_array($result)){
echo '<div id="delete" align="center">';
echo '<img src="'.$row['image'].'" style="border:1px solid black; width:100px;"><br>Delete';
echo '</div>';
}
echo '</div>';
Put overflow: hidden; CSS rule on the div which has the overlapping content in it.
try clearing the div inside the while loop, maybe something like
while($row = mysql_fetch_array($result)){
echo '<div id="delete" align="center">';
echo '<img src="'.$row['image'].'" style="border:1px solid black; width:100px;"><br>Delete';
echo '<div style="clear:both"></div></div>';
}
may help?
or declare the <a> element as a block element?
Using a PHP loop is not related to this issue, what matters is the HTML code that is served to your web browser.
Try adding a clear: both; style to the div in question:
<div id="delete" align="center" style="clear: both;">
You may also align using CSS instead of a HTML attribute:
<div id="delete" style="text-align: center; clear: both;">
UPDATE I see that your DIV elements are overleaping, not floating next to each other as I originally thought.
Check your CSS and see if you can find any position: absolute that applies to these DIV elements, and remove it. In addition, you should use class="delete" instead of id="delete", as multiple elements should not share the same id attribute.
For testing, you can try:
<div id="delete" style="text-align: center; position: block;">

Categories