How to style PHP echo output with span - php

I'm trying to style the output of each echo.
Ideally I'd like to use <span class=""> </span> for each echo, but I'm not too sure how to achieve this.
$result = mysql_query("SELECT * FROM Blog");
while($row = mysql_fetch_array($result))
{
echo $row['Date'];
echo $row['Title'];
echo $row['Message'];
echo "<img src='".$row['Image']."'/>";
}
mysql_close($con);

$result = mysql_query("SELECT * FROM Blog");
while($row = mysql_fetch_array($result))
{
echo "<span class=\"myclass\">$row['Date']</span>";
echo "<span class=\"myclass\">$row['Title']</span>";
echo "<span class=\"myclass\">$row['Message']</span>";
echo "<img src='".$row['Image']."'/>";
}
mysql_close($con);
or, much nicer, in a table:
$result = mysql_query("SELECT * FROM Blog");
echo "<table>"
while($row = mysql_fetch_array($result)) {
echo "<tr>"
echo "<td>$row['Date']</td>";
echo "<td>$row['Title']</td>";
echo "<td>$row['Message']</td>";
echo "<td><img src='".$row['Image']."'/></td>";
echo "</tr>"
}
echo "</table>"
mysql_close($con);
You then can style each row and column with a class.

Try this:
$prepend = "<span class=''>";
$append = "</span>";
$result = mysql_query("SELECT * FROM Blog");
while($row = mysql_fetch_array($result))
{
echo $prepend.$row['Date'].$append;
echo $prepend.$row['Title'].$append;
echo $prepend. $row['Message'].$append;
echo $prepend."<img src='".$row['Image']."'/>".$append;
}
mysql_close($con);

I'd create a function that does this:
function decorated_echo($text) {
echo '<span class="myclass">' . $text . '</span>';
}
This way, you don't have to repeat this everytime you want this behaviour.

You are guessing right, just add required html in the echo:
echo '<span class="yourclass"><img src="'.$row['Image'].'" /></span>';
or you can just put inline style if no css file is loaded:
echo '<span style="color:red;"><img src="'.$row['Image'].'" /></span>';

Related

Why I can't echo div?

I have code for list values from database but when I add them to echo "<div>" it stops working
My code:
$sql = "SELECT name, size, type, username FROM Files";
$result = $conn->query($sql);
if ($result->num_rows > 0)
{
while($row = $result->fetch_assoc())
{
echo "<div class='filediv'>";
echo "<a href='uploads/$row['name']' download>$row['name']</a>";
echo "<p>Size: $row['size']KB</p>";
echo "<p>Type: $row['type']</p>";
echo "<p>Creator: $row['username']</p>";
echo "</div>";
echo "<hr>";
}
}
you have a syntax error
echo "<div class='filediv'>";
echo "<a href='uploads/".$row['name']."' download>".$row['name']."</a>";
echo "<p>Size:". $row['size']."KB</p>";
echo "<p>Type: ".$row['type']."</p>";
echo "<p>Creator: ".$row['username']."</p>";
echo "</div>";
echo "<hr>";
You can use {} for print php variables in html.
echo "<div class='filediv'>";
echo "<a href='uploads/{$row['name']}' download>{$row['name']}</a>";
echo "<p>Size: {$row['size']}KB</p>";
echo "<p>Type: {$row['type']}</p>";
echo "<p>Creator: {$row['username']}</p>";
echo "</div>";
echo "<hr>";

Output three colums from my mysql database php

I'm a beginner in php and i'd like to output 3 fields from my database to my website which included a bloc image,text and name. using a foreach i can only get 2 out of the three.
<table border="2px">
<tr>
<th> Brand</th>
<th> Images</th>
<th> Details</th>
</tr>
<?php
$tex = [];
$query = mysqli_query($conn, "SELECT * FROM upload");
while ($row = mysqli_fetch_assoc($query)) {
$images[] = $row['image'];
$br[] = $row['name'];
$det[] = $row['text'];
}
foreach (array_combine($det, $images) as $text => $image) {
echo "<tr><td>";
echo $name;
echo "</td>";
echo "<td>";
echo '<img src="data:image/jpeg;base64,'.base64_encode($image).'"/>';
echo "<td>";
echo $text;
echo "</td>";
echo "</td></tr>";
}
?>
</table>
that is a snippet of my code where images and text is outputted on the screen and receive and undefined variable in error in line 39 which would be echo $name;. what should i put in the foreach statement to get it to output all three?
$images[] = $row['image'];
$br[] = $row['name'];
$det[] = $row['text'];
can someone explain these to me as i have an idea what it is doing but i want to be 100.
This would work:
while ($row = mysqli_fetch_assoc($query)) {
$images[] = $row['image'];
$br[] = $row['name'];
$det[] = $row['text'];
}
foreach ($images as $idx => $image) {
echo "<tr><td>";
echo $br[$idx];
echo "</td>";
echo "<td>";
echo '<img src="data:image/jpeg;base64,'.base64_encode($image).'"/>';
echo "<td>";
echo $det[$idx];
echo "</td>";
echo "</td></tr>";
}
You are combining only text and image. That way you would not know which name is the corresponding one. My loop solves this by using the index ($idx) for that. All arrays have the same size, so this works.
There's no point using a separate foreach loop like that. You should simply change your while loop like this:
while($row = mysqli_fetch_assoc($query)){
echo"<tr><td>";
echo $row['name'];
echo "</td>";
echo "<td>";
echo '<img src="data:image/jpeg;base64,'. base64_encode($row['image']) .'"/>';
echo"<td>";
echo $row['text'];
echo "</td>";
echo"</td></tr>";
}

Adding two fields in php

$result = mysql_query("SELECT * FROM productlist1 order by pdesc ASC");
while($row = mysql_fetch_array($result))
$sold=$row['psold'];
$left=$row['pleft'];
$all=$left + $sold;
{
echo '<tr>';
echo '<td>'.$row['pcode'].'</td>';
echo '<td>'.$row['pdesc'].'</td>';
echo '<td>'.$row['date'].'</td>';
echo '<td>'.$row['time'].'</td>';
echo '<td><div align="center">'.$row['psold'].'</div></td>';
echo '<td><div align="center">'.$row['pleft'].'</div></td>';
echo '<td><div align="center">'.$row['pprice'].'</div></td>';
echo '<td><div align="center">'.$all.'</div></td>';
i want to add the pleft and psold, the $all doesn't work at all... what would i do with this code?
You have a syntax error. You need to place following 3 lines inside brackets:
$sold=$row['psold'];
$left=$row['pleft'];
$all=$left + $sold;
like:
while($row = mysql_fetch_array($result))
{
$sold=$row['psold'];
$left=$row['pleft'];
$all=$left + $sold;
echo '<tr>';
....
You have to put opening braces for while loop just after the while statement like.
while($row = mysql_fetch_array($result))
{
$sold=$row['psold'];
$left=$row['pleft'];
$all=$left + $sold;
...............
Put your addition inside while loop,
because outside while loop it doesn't work, because it does not get proper value
So , try like this
<?php
$result = mysql_query("SELECT * FROM productlist1 order by pdesc ASC");
while($row = mysql_fetch_array($result))
{
$all=$row['psold'] + $row['pleft'];
echo '<tr>';
echo '<td>'.$row['pcode'].'</td>';
echo '<td>'.$row['pdesc'].'</td>';
echo '<td>'.$row['date'].'</td>';
echo '<td>'.$row['time'].'</td>';
echo '<td><div align="center">'.$row['psold'].'</div></td>';
echo '<td><div align="center">'.$row['pleft'].'</div></td>';
echo '<td><div align="center">'.$row['pprice'].'</div></td>';
echo '<td><div align="center">'.$all.'</div></td>';
Try this.
$result = mysql_query("SELECT * FROM productlist1 order by pdesc ASC");
while($row = mysql_fetch_array($result))
{
$sold=$row['psold'];
$left=$row['pleft'];
$all=$left + $sold;
echo '<tr>';
echo '<td>'.$row['pcode'].'</td>';
echo '<td>'.$row['pdesc'].'</td>';
echo '<td>'.$row['date'].'</td>';
echo '<td>'.$row['time'].'</td>';
echo '<td><div align="center">'.$row['psold'].'</div></td>';
echo '<td><div align="center">'.$row['pleft'].'</div></td>';
echo '<td><div align="center">'.$row['pprice'].'</div></td>';
echo '<td><div align="center">'.$all.'</div></td>';
echo '</tr>';
}
Try parsing the values to integer like
$all=intval($left)+intval($sold);
Yeah one more thing your while is misplaced!!

Passing PHP select elements into a url

Ok so I have a simple php select script and I have the code below to define variables.
$result = mysqli_query($con,"SELECT * FROM practice_sheets WHERE student_name='$_SESSION[SESS_FIRST_NAME] $_SESSION[SESS_LAST_NAME]'");
$numrows = mysqli_num_rows($result);
$id = $row['id'];
$total_min = $row['total_min'];
$due_date = $row['due_date'];
I then have:
echo "<td> <a href='account/practiceSheets?id='$id'> <i class='icon-eye-open'> </i> </a> </td>";
and this is supposed to pass the variables from the php select script into the url when the <a> is clicked.
All I end up with is the account/practiceSheets?id= with no actual id. I'm sure this is something stupidly simple and I do apologize as I am new to PHP and also didn't know what to call this to get a useable result in search engines! My full code is below if it helps.
<?php
$con = mysqli_connect("50.63.106.47", "usd309bands", "MacBook1!", "usd309bands");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con, "SELECT * FROM practice_sheets
WHERE student_name='$_SESSION[SESS_FIRST_NAME] $_SESSION[SESS_LAST_NAME]'");
$numrows = mysqli_num_rows($result);
$id = $row['id'];
$total_min = $row['total_min'];
$due_date = $row['due_date'];
if ($numrows == 0) {
echo "<div class='alert alert-danger'>";
echo "No Entries, See your instructor for details.";
echo "</div>";
} else {
echo "<table class='mws-table table-striped table-hover'>";
echo "<thead align='center'>";
echo "<tr>";
echo "<th>Sheet Number</th>";
echo "<th>Total Minutes</th>";
echo "<th>Due Date</th>";
echo "<th>View</th>";
echo "</tr>";
echo "</thead>";
echo "<tbody align='center'>";
while ($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['total_min'] . "</td>";
echo "<td>" . $row['due_date'] . "</td>";
echo "<td> <a href='account/practiceSheets?id='$id'> <i class='icon-eye-open'> </i> </a> </td>";
echo "</tr>";
}
echo "</tbody>";
echo "</table>";
mysqli_close($con);
}
?>
$result = mysqli_query($con,"SELECT * FROM practice_sheets
WHERE student_name='$_SESSION[SESS_FIRST_NAME] $_SESSION[SESS_LAST_NAME]'");
$numrows = mysqli_num_rows($result);
$id = $row['id'];
Can't see the where the $row is coming from.
I think you've forgotten to fetch the MySQL response into an associated array.
(You could use mysqli_fetch_assoc() to acomplish that)
Here's a quick example:
$result = mysqli_query($con,"SELECT * FROM practice_sheets WHERE student_name='$_SESSION[SESS_FIRST_NAME] $_SESSION[SESS_LAST_NAME]'");
$allRows = mysqli_fetch_assoc($result);
foreach($allRows as $row) {
echo $row['id'].'<br/>';
}
This one should list all ID's from the database request.
This sql query is probably not going to return anything unless names are stored in your table like this
firstlast
"SELECT * FROM practice_sheets WHERE student_name='$_SESSION[SESS_FIRST_NAME] $_SESSION[SESS_LAST_NAME]'"
you probably want something like this
$_SESSION[SESS_FIRST_NAME] . " " . $_SESSION[SESS_LAST_NAME]
to return a name like
first last
I changed
echo "<td> <a href='account/practiceSheets?id='$id'> <i class='icon-eye-open'></i></a></td>"
to this:
echo "<td> <a href='account/practiceSheets?id=" . $row["id"] . "'> <i class='icon-eye-open'> </i> </a> </td>";
and got the desired results!

Font Color Issue While Using in Mysql commands

I used the font color red, but in my page it showing green color.
<?php
include('connect-db.php');
$result = mysql_query("SELECT * FROM players") or die(mysql_error());
while($row = mysql_fetch_array( $result )) {
echo '<font color=\"red\">';
echo $row['firstname'] ;
echo '</font>';
echo ':';
echo $row['lastname'] ;
echo '<br><br>';
}
?>
<?php
include('connect-db.php');
$result = mysql_query("SELECT * FROM players") or die(mysql_error());
while($row = mysql_fetch_array( $result )) {
echo '<span style=\"color:red;\">';
echo $row['firstname'] ;
echo '</span>';
echo ':';
echo $row['lastname'] ;
echo '<br><br>';
}
?>

Categories