Conditional PHP styling based on variable value - php

I have some PHP code which executes and selects 10 rows from a SQL table. One column called result can hold the value of won, or loss.
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
echo "<div class = 'logrow'> <img src ='". $row["url"] ."'</img> <p class = 'logtext'>". $row["name"] ." bet $". $row["amount"] ." with a ". $row["chance"] ."% chance and ". $row["result"] .". </p> </div>";
}
How could I do something which would echo the values from each row as above but to echo a different statement for each row where the value of result is loss. For example to add an inline styling for the background colour.
So for example, say I have 10 rows found - 9 of these have $row["result"]as won, so they should be echoed as above. But 1 row has the value of $row["result"] as loss, a different echo should be applied. Perhaps with an inline style, or maybe with a variable inserted which hold this style.
I know this is very specific and may not be clear so thanks in advance.

Based on the value in $row["result"] (won/loss) create a class and use it in the echo
css:
.won{background-color::blue}
.loss{background-color::red}
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
echo "<div class = 'logrow ". $row["result"] ."'> <img src ='". $row["url"] ."'</img> <p class = 'logtext'>". $row["name"] ." bet $". $row["amount"] ." with a ". $row["chance"] ."% chance and ". $row["result"] .". </p> </div>";
}

Try this by changing class names.
<?php
$status = $row['result'];
$classname = 'won';
if($status == 0){
$classname = 'fail';
}
?>
<div class = 'logrow <?php echo $classname ?> '> <img src ='". $row["url"] ."'</img> <p class = 'logtext'>". $row["name"] ." bet $". $row["amount"] ." with a ". $row["chance"] ."% chance and ". $row["result"] .". </p> </div>";
Now define classes for won and fail under style sheet.
<style type="text/css">
.won{ color: green; }
.fail{ color: red; }
</style>

Related

Tinymce echo Images next to each other instead below

I have a MYSQL database where I store images for reports I can import them to the Tinymce text area but they are being placed below each other. Is there away I can make them place two two next to each other. Some reports have 2 photos and other might have 10 depending on how many was loaded to the DB
This is my current code:
$result = mysqli_query($db, "SELECT * FROM images WHERE file_nr = '$id'");
?>
<?php
while ($row = mysqli_fetch_array($result)) {
echo "<div id='img_div'>";
echo "<img width='300' height='300' display: inline-block;
src='../../cases_1/overdue/images/".$row['image']."' >";
echo "<p>".$row['image_text']."</p>";
echo "</div>";
}
?>
I tried inline-block not working.
I don't think this is the cleanest method but got this working
<?php
while ($row = mysqli_fetch_array($result)) {
echo "<div id='img_div'>";
echo "<img width='350px' height='350px' style='float: left; padding: 4px;' src='../../cases_1/overdue/images/".$row['image']."' >";
echo "</div>";
}
?>

PHP / MySQL - Query not fetching any data

I have been trying to set up a page that lists prices of items from a table in a database. Here is my code:
<?php
$querylist = mysql_query("SELECT item_name,image,price,added_by FROM values");
while($row = mysql_fetch_array($querylist))
{
echo '<div class="post rareitem" style="margin-right: 15px;float: left;">
<div class="rarename">
<strong>';
// Shows Item Name
echo $row['item_name'];
echo '</strong>
</div>';
// Shows Item Image
echo '<div class="rareimage" style="background-image: url(/app/tpl/skins/Mango/images/values/rares/';
echo $row['image'];
echo ');"></div>';
// Shows Item Price
echo '<div class="rarecontrols">
<div class="coinsbox"></div>
<span>
<b> <b>Credits: </b> </b> ';
echo $row['price'];
echo '</span>';
// Shows Who Added the Item
echo '<div class="addedbox"></div><b><b><span><font color="#c93734"><font color="#c93734">Added By: </font> </font>';
echo $row['added_by'];
echo '</span></b></b>
</div>
<div class="clear"></div>
</div>';
}
?>
There is another chunk of code (shown below) that I have based this off of, and it works perfectly fine. I can't seem to get this to work though. I believe it has something to do with the SQL, the syntax, or something. No matter what I do, it produces absolutely no results, yet the code below results exactly as planned. I know for a fact it is not a connection issue because the below code can be placed on the same exact page as the above one and it works fine, however the above does not.
<?php
$querylist = mysql_query("SELECT id,username,motto,country,look,rank,account_created,role,account_created,online,last_online FROM users WHERE rank='9' ORDER BY ID LIMIT 20");
while($row = mysql_fetch_array($querylist))
{
echo '
<div class="team">';
// Showing Avatar
echo '<div style="float: left; margin-top:-1px;height: 60px; width: 64px; background: url(http://www.habbo.nl/habbo-imaging/avatarimage?figure=';
echo $row['look'];echo "&action=wav&direction=3&head_direction=3&gesture=sml&size=m) no-repeat 0px -10px";
echo "\"/>";
echo "<img alt=\"badge\" src=\"/app/tpl/skins/habbo/images/icons/";
echo $row['online'];echo ".gif\"></div>";
// Flags
echo "<br/><img src=\"/app/tpl/skins/habbo/images/icons/flags/";
echo $row['country'];echo ".png";
echo '" style="float:right;" /> <b><uf>';
echo $row['username'];echo "</u></b>";
// Bans & Ticket Count
$Bans = mysql_query("SELECT * FROM `bans` WHERE `added_by` = '" . $row['username'] . "'");
$BanCount = mysql_num_rows($Bans);
$Tickets = mysql_query("SELECT * FROM `moderation_tickets` WHERE `moderator_id` = '" . $row['id'] . "'");
$TicketCount = mysql_num_rows($Tickets);
//Role
echo "<br/><gb>Role: </b><fi> ";
echo $row['role'];echo "";
echo "</i>";
// Echoing bans & Tickets
echo "<br/><gb>Bans: </b><fi> ";
; echo $BanCount;
echo "</i>";
echo " <gb>Tickets: </b><if>";
; echo $TicketCount;
echo "</i>";
echo "</div>";
}
?>
Thanks in advanced, any assistance will be greatly appreciated!
values is Reserved Words in mysql it should be on backtick
SELECT item_name,image,price,added_by FROM `values`
And stop using mysql it is deprecated. Instead use mysqli or PDO

Filepath from SQL database not looping a BG image

I have an Image uploaded to a folder with the filepath stored in a table along with other info (title, description). I would like this info looping inside a repeated div. Both other variables loop correctly, however the filepath variable needs to be used in a background URL and at the moment only echos the value of the last row. Thank you very much for your help, there has got to be a simple solution! -Sean
<?php
$result = mysql_query("SELECT * FROM `program`");
$values = mysql_fetch_array($result);
$globals['filepath'] = $row['filepath'];
echo "<div>"; // start a table tag in the HTML
while($row = mysql_fetch_array($result)){ //Creates a loop to loop through results
echo "<div class=wrapper3 >
<h2>" . $row['program_name'] . "</h2>
<p>" . $row['program_description'] . "</p>
</div>
<p> </br> </p>";
$globals['bgimage'] = $row['filepath'];
}
echo "</div>";
?>
<style type="text/css">
.wrapper3{
width:85%;
margin:0 auto;
padding:20px;
height:auto;
color:#FFF;
background: url(/SMLC/<?php while ($row = mysql_fetch_array($result)){ echo $globals['bgimage'];} ?>
) no-repeat;
background-size:cover;
color:#000;
height:250px;
text-align:center;
background-color:#fff;
border-radius:6px;
border:1px solid #0FF;
}
</style>
<?php mysql_close();?>
Remove the background from the .wrapper3 in css, and add it to the element as an inline style.
<?php
$result = mysql_query("SELECT * FROM `program`");
echo "<div>"; // start a table tag in the HTML
while ($row = mysql_fetch_array($result)) { //Creates a loop to loop through results
$globals['filepath'] = $row['filepath'];
echo "<div class='wrapper3' style=\"background: url('/SMLC/".$row["filepath"]."');\">
<h2>" . $row['program_name'] . "</h2>
<p>" . $row['program_description'] . "</p>
</div>
<p></br></p>";
}
echo "</div>";
?>
NOTE: Use mysqli functions or PDO instead of mysql functions, since, mysql functions are deprecated.

how to use get method in a session?

here is my code. actually i am displaying some data from mysql on the page and creating dynamic link.i want started a session with session_start() in the very begining of code before starting any code. i want to store the value of the link that is to be display on other pagepage..
page1.php
<a style="color:#F00; font-family:Arial, Helvetica, sans-serif; margin-left:33px; font-weight:bold">
No. of registered students:
</a>
<table border='1' align="center" style="font-size:14px" width="95%" cellspacing="3" class="db_table">
<tr class="db_table_tr" >
<th class="db_table_th" name="submit">USN</th>
</tr>
<?php
include('includes/login_connection.php');
$query = "select p.usn, p.name from personal_details p, course_codes c where p.usn = c.usn order by p.usn";
$run = mysql_query($query) or die($query."<br/><br/>".mysql_error());
$num = mysql_numrows($run);
echo $num;
while($row = mysql_fetch_assoc($run)){
echo "<tr>";
echo "<td>" . $row['usn'] . "" . "</td>";
echo "<td>" . $row['name'] . " </td>";
if(isset($_GET['submit'])){
$_SESSION['session_usn'] = $_GET['usn'];
}
}
echo "</tr>";
mysql_close($bd);
?>
</table>
page2.php
<?php
session_start();
if(isset($_SESSION['session_usn']))
{
$_POST['usn'] = $_SESSION['session_usn'];
echo $_POST['usn'];
}
?>
You need to provide a fall-back, in case the URL provided does not contain the proper variables in the $_GET section.
You have:
if(isset($_GET['submit'])){
$_SESSION['session_usn'] = $_GET['usn'];
}
You should do something else if $_GET['submit'] isn't set:
if(isset($_GET['submit'])){
$_SESSION['session_usn'] = $_GET['usn'];
} else {
$_SESSION['session_usn'] = "unset";
// or set a warning flag like "unset"
}
You should be feeding your php file a url like:
http://yoururl.com/page1.php?usn='333'
Where 333 is the value you want to store.

Bolding a specifc variable in PHP throughout a page

<?php
# session
session_start();
# check that session is set and is valid
if(!isset($_SESSION['login']))
{ header('Location: login.php');
}
?>
<body>
<div class="maincontainer">
<div class="keywordhead">
<div align="center"><img src="Images/keyword_title.png" width="243" height="56" /></div>
</div>
<div class="results">
<p>
<?php
$kword = $_POST["kword"];
function boldText($text, $kword) {
return str_replace($kword, "<strong>$kword</strong>", $text);
}
$testin1 = substr($kword,0,1);
if($testin1 == "") {
print "<strong>No Keyword or a Keyphrase Entered, Please return to the '<a href='keyword_search.php'>Keyword Search Page</a>'</strong>";
}
else {
// Connects to your Database
mysql_connect("localhost", "root") or die(mysql_error());
mysql_select_db("test") or die(mysql_error());
}
mysql_real_escape_string($kword);
$data = mysql_query("select company_name, section_name, question, answer from company, rfp, section, question_keywords
where company.company_id = rfp.company_id
and rfp.rfp_id = question_keywords.rfp_id
and question_keywords.section_id = section.section_id
and keywords like '%$kword%';")
or die(mysql_error());
echo "<table border=0 cellpadding=10>";
echo "<tr align = center bgcolor=white>
<td><b>Company Name</b></td><td><b>Section</b></td><td><b>Question</b></td><td><b>Answer</b></td>" ;
while($info = mysql_fetch_array( $data ))
{
echo "<tr>";
echo "<td width = 130px>".boldText($info['company_name'], $kword) . "</td> ";
echo "<td width = 60px>".boldText($info['section_name'], $kword) . " </td>";
echo "<td width = 300px>".boldText($info['question'], $kword) . " </td>";
echo "<td width = 600px>".boldText($info['answer'], $kword) . " </td></tr>";
}
echo "</table>";
?>
</p>
</div>
<div class="footer"><a href="logout.php"><br />
Logout</a> | Index | Back</div>
</div>
</body>
</html>
I am relatively new to PHP, and i was curious as to whether a certain function is possible. I have a keyword Search and the code for the results page is above. I would like to bold wherever the $kword variable appears on the page. is this possible?
Thanks
You can create a function to do so, and call it prior to echo'ing the variables.
Instead of: $info['question'] use boldText($info['question'], $kword)
function boldText($text, $keyword) {
return str_ireplace($keyword, "<strong>$keyword</strong>", $text);
}
As a side note, don't forget to escape $kword with mysql_real_escape_string() before using it in a SQL query, or even better, consider using MySQLi or PDO since mysql extension is strongly discouraged
Can you use something like this in each of your Print statements?
str_replace($kword, "<b>$kword</b>", $info[...])
(or CSS e.g.
<span style='font-weight:bold'>...</span>
if you prefer).

Categories