styling mysql connect page - php

does anybody know how to style the 'echo' tags on my page? i am pulling from a mysql table, but cannot figure out how to style the individual rows. any help would be appreciated. here is my code:
<?
$con=mysqli_connect("localhost","ameriex9_eric","fitness2003","ameriex9_blog");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM blog_entries ORDER BY date DESC");
while($row = mysqli_fetch_array($result))
{
echo $row['name'];
echo "<br>" ;
echo $row['date'] ;
echo "<br>";
echo $row['title'];
echo "<br>";
echo $row['blog'];
echo "<br>";
echo $row['pics'];
echo "<hr>";
}
mysqli_close($con);
?>

You can style the individual rows like this, for example:
echo "<span class=\"myclass\">{$row['name']}</span>";
where .myClass can be a class somewhere in your CSS file.
This would work too:
echo '<span class="myClass">Name' . $row['name'] . '</span><br>';
Inline styles is possible too, though being more messier:
echo '<span style="color:red; font-size:12px;">Name' . $row['name'] . '</span><br>';

We can add a small style rule
<style scoped>
table,td{ border:1px solid #f4f4f4; border-collapse:collapse; padding:10px; }
</style>
and slightly modify your code around while loop like this...
echo "<table>";
while($row = mysqli_fetch_array($result))
{
echo "<tr><td>".$row['name']."</td><td>".$row['date']."</td><td>".$row['blog']."</td><td>".$row['pics']."</td></tr>";
}
echo "</table>";

Related

Awkward white space above outputed Table

I outputed the data from my database in a HTML Table and styled it with CSS. Everything is fine, but there is an awkward White space above the outputed Table. (see picture). What's wrong with my code?
Result:
<html>
<head>
<title>Create new user</title>
<style>
table {
border-collapse: collapse;
width: 100%;
}
th, td {
text-align: left;
padding: 8px;
}
tr:nth-child(even){background-color: #f2f2f2}
th {
background-color: #4CAF50;
color: white;
}
</style>
</head>
<body>
<div>
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "users";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "select * from employee";
$result = $conn->query($sql);
echo "<table>";
echo "<th>Identifier</th><th>Name</th><th>Lastname</th>";
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "<tr>";
echo "<td>" . $row["id"] . "</td>";
echo "<td>" . $row["vorname"] . "</td>";
echo "<td>" . $row["nachname"] . "</td>";
echo "</tr>";
echo "</br>";
}
}
else {
echo "0 results";
}
echo "</table>";
$conn->close();
?>
</div>
</body>
</html>
Expected result : no white space above the Table
Many Thanks
Why are you adding a <br>
echo "</br>";
in your while loop.
Try this -
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "<tr>";
echo "<td>" . $row["id"] . "</td>";
echo "<td>" . $row["vorname"] . "</td>";
echo "<td>" . $row["nachname"] . "</td>";
echo "</tr>";
}
} else {
echo "0 results";
}
If this does not fix, try inspecting and see what is adding space. Also check for the padding and margins in the computed view in chrome inspect view.
If nothing works, try setting top:0 for the div and may be for table :)

How to create a dynamic php page

So it's actually hard to explain what I mean.
So I have this list here. Its pulls data from the db and then displays it in a DIV on my webpage.
I want it to only show the first 4 listing. Then after 4 seconds. I want it to show the next 4 listening and keep revolving like that infinitely. If someone could point me in the direction to be able to do that. Also, It needs to be updating dynamically every 4 seconds. Not when the user hits refresh.
<?php session_start(); ?>
<head>
<link rel="stylesheet" type="text/css" href="tablecss.css">
<style type="text/css">
.extra {
position: relative;
top: 50px;
}
body {
overflow: hidden;
}
</style>
</head>
<body>
<?php
$con = mysqli_connect('##', '##', '##', '##');
if(mysqli_connect_errno()) {
echo 'Failed to Connect to MySQL' . mysqli_connect_errno();
}
$user = $_SESSION['username'];
$result = mysqli_query($con, "SELECT * FROM corporations");
echo "<table id='tb'border='1'>
<tr>
<th>Name</th>
<th>Owner</th>
<th>Last Price</th>
<th>Volume</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['name']. "</td>";
echo "<br>";
echo "<td>" . $row['owner'] . "</td>";
echo "<br>";
echo "<td>" .'$'. $row['shareValue'] . "</td>";
echo "<br>";
echo "<td>" . $row['publicShares'] . "</td>";
echo "<br>";
}
mysqli_close($con);
?>
</div>
</body>
What you can do is javascript, and use ajax. to load it every four seconds you can use setInterval(http://www.w3schools.com/jsref/met_win_setinterval.asp), but for me its better if you use jQuery load more data on scroll.
to limit output use the limit of mysql.
see this article: http://www.mysqltutorial.org/mysql-limit.aspx

Lightbox not showing php

I'm working for a system by the use of php. We have an admin.php page where all the list of applicants were listed. And we want that when we click on a specific applicant's name, a lightbox would appear that will show more information about the applicant. But, these codes below seems to be not working and I don't know why. I am a newbie in php btw.
<?php
include('config.php');
$result = mysql_query("SELECT * FROM employee as t1")
or die(mysql_error());
echo "<table border='0' cellpadding='15' text-align = 'center' >";
echo "<tr>";
echo " <caption><b><font size = '5'> <h2>List of Applicants </h2> </b> </font><hr></caption>";
echo "<th>Applicant ID</th>";
echo "<th>Application Date</th>";
echo "<th>Name</th>";
echo "<th>Job</th>";
echo "</tr>";
while($row = mysql_fetch_array( $result ))
{
echo "<tr class='gradeC'>";
echo '<td><b><font color="#663300">' . $row['employee_id'] . '</font></b></td>';
echo '<td><b><font color="#663300">' . $row['date'] . '</font></b></td>';
echo '<td><b><font color="#663300">' . $row['fname'] . '</font></b></td>';
echo '<td><td>';
echo "</tr>";
}
echo "</table>";
function runMyFunction() {
echo "<div onclick =\"document.getElementById('light').style.display='block';document.getElementById('fade').style.display='block'\"></div>";
echo "<div id=\"light\" class=\"white_content\">This is the lightbox content. Close</div>";
echo "<div id=\"fade\" class=\"black_overlay\"></div>";
}
if (isset($_GET['hello'])) {
runMyFunction();
}
?>
I'm not sure but I think the problem is in the function, or the way I'm using the codes for lightbox to appear.
Can you rewrite this line
function runMyFunction() {
echo "<div onclick =\"document.getElementById('light').style.display='block';document.getElementById('fade').style.display='block'\"></div>";
echo "<div id=\"light\" class=\"white_content\">This is the lightbox content. Close</div>";
echo "<div id=\"fade\" class=\"black_overlay\"></div>";
}
to
function runMyFunction() {
?>
<div onclick ="document.getElementById('light').style.display='block';document.getElementById('fade').style.display='block'"></div>
<div id="light" class="white_content">This is the lightbox content.
Close</div>
<div id="fade" class="black_overlay"></div>
<?php }
By eliminating the slashes could help make the code easier to process.
I hope this help.

Style 1 <td> in a table with 2 <td>s

I have a table that gets it´s content from a database, and I want to make the text in the column to the right align-right. I know how to do it when its HTML and the content is in the code, but I can't get it to work when the content is outputted from a database.
Down here you can see my code for the table: (in the collumn "ingredienser", I want to set the text-align to right.
<?php
$con = mysql_connect("****","****","****");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("paj", $con);
$result = mysql_query("SELECT * FROM meny");
echo "<div id='menyer'>";
echo "<table border='1'>
<tr>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<div class='mall" . $row['mall'] . "'>";
echo "<tr>";
echo "<td>" . $row['namn'] . "</td>";
echo "<td>" . $row['ingredienser'] . "'</td>";
echo "</tr>";
Just add some CSS above your php code
<style>
table td {
text-align: right;
}
</style>
The above will right align text inside table cells
echo "<td><div align='right'>" . $row['ingredienser'] . "'</div></td>";
if this is the extent of your table, then you can definitely just use the following css:
.mall{
text-align:right;
}
I would suggest putting it in your external css file, but if you don't have one, then put it in a style tag inside of your body or table tag, depending on what kind of scope you want it to have like this:
<style>
.mall{
text-align:right;
}
</style>
set align="right" to your tr
while($row = mysql_fetch_array($result))
{
echo "<div class='mall" . $row['mall'] . "'>";
echo "<tr align='right'>";
echo "<td>" . $row['namn'] . "</td>";
echo "<td>" . $row['ingredienser'] . "'</td>";
echo "</tr>";
Just add styling to that one particular cell, it will apply to the entire column as it loops. As such,
echo "<td style=\"text-align:right;\">" . $row['ingredienser'] . "'</td>";
If you align the Table TD to text-align:right, EVERY single cell will be effected.

How to give alternating table rows different background colors using PHP

I have a table of data that is generated dynamically based on the contents stored in a mysql database.
This is how my code looks:
<table border="1">
<tr>
<th>Name</th>
<th>Description</th>
<th>URL</th>
</tr>
<?php
$query = mysql_query("SELECT * FROM categories");
while ($row = mysql_fetch_assoc($query)) {
$catName = $row['name'];
$catDes = $row['description'];
$catUrl = $row['url'];
echo "<tr class=''>";
echo "<td>$catName</td>";
echo "<td>$catDes</td>";
echo "<td>$catUrl</td>";
echo "</tr>";
}
?>
</table>
Now if the table was static, then I would just assign each alternating table row one of 2 styles in repeated order:
.whiteBackground { background-color: #fff; }
.grayBackground { background-color: #ccc; }
and that would be the end of that. However since the table rows are dynamically generated, how can I achieve this?
Or you could just use CSS:
table tr:nth-child(odd) {
background-color: #ccc;
}
<?php
$x++;
$class = ($x%2 == 0)? 'whiteBackground': 'grayBackground';
echo "<tr class='$class'>";
?>
It basically checks to see if $x is divisible evenly by 2. If it is, it is even.
P.S. if you haven't seen that style of if else query, it is called a ternary operator.
Set a variable to true/false or a number and then back again during each iteration. Or use the modulus operator such as $i%2==0 in a while loop where $i is a number and use this condition in a ternary statement or something that sets the class value of the <tr>
Easiest way to alternate row colors in PHP/HTML?
$i = 0;
while ( $row = mysql_fetch_assoc($result) ) {
echo '<tr class="' . ( ( $i %2 == 0 ) ? 'oneValue' : 'anotherValue' ) . '"><td>' . $row['something'] . '</td></tr>';
$i++;
}
<?
$color="1";
while ($line = mysql_fetch_array($result)) {
if($color==1){
echo '<tr bgcolor="">';
$color="2";
} else {
echo '<tr bgcolor="#dcdcdc">';
$color="1";
}
?><td align="left" width="40"><?= $line[name] ?></td>
<?
}
?>
This is my working code !
Here is my working part !
`
$i=1;
while($row = mysqli_fetch_array($result)) {
if($i%2==0)
{
echo '<tr bgcolor="#FFFF00">';
}
else
{
echo '<tr bgcolor="#99FFCC">';
}
$i++;
echo "<td>" . $row['blah'] . "</td>";
echo "<td>" . $row['blah_blah'] . "</td>";
echo "</tr>";
}
echo "</table>";
`
This is how I did it. I declared a css class called "even" with all the styling i wanted. Then looped through the scenario. Hope it helps!
<?php
include 'connect.php';
echo "<table id='hor-zebra'>";
$i = 0;
while($row = mysql_fetch_array($result))
{
if($i%2 == 0)
{
echo "<tr class='even'>";
echo "<td>" . $row['something'] ." ". $row['something'] . "</td>";
echo "</tr>";
}
else
{
echo "<tr>";
echo "<td>" . $row['something'] ." ". $row['something'] . "</td>";
echo "</tr>";
}
$i++;
}
echo "</table>";
mysql_close($con);
?>

Categories