I want to set the color of the variable to red or green depending on the condition of the variable , it should appear red or green if its more and less than 0 respectively. But , the echo-ing is not allowing me to do so in the sense that the profitandloss echoed each row the has no color set.Because the rest of the row is all retrieved from database .So i would also need to echo it out. I know the method im doing is wrong. The rows are echoed on a row by row basis so the color is dependent on the value. So how do i get the colors to work by a per row basis??
if($profitandloss<$zero){
"<div style=\"color: red;\">$profitandloss</div>";
}
elseif ($profitandloss>$zero)
"<div style=\"color: green;\">$profitandloss</div>";
// }
// for profit and loss counting
//
echo "<tr><td>" . $row['trade_id'] . "</td><td>" . $row['selection'] . "</td><td>" . $row['date'] ."</td><td>" . $row['type'] ."</td><td>" . $row['size'] ."</td><td>" . $row['bidprice'] ."</td><td>" . $row['offerprice'] ."</td><td>" . $row['stoploss'] ."</td><td>" . $row['takeprofit'] ."</td><td>" . $profitandloss . "</td><td><a href ='delete.php?id=".$row['trade_id']."'>X</a></td></tr>";
You're not echoeing anything there. Your PHP script is full of errors. You can't just mix and match HTML and PHP this way.
When doing IF statements, you need to 'echo' (or store in variables) the HTML parts inside a string, and concatenate the variables to them. This way the page will display only the value of the PHP variables when rendered into HTML
if($profitandloss<$zero){
$red_div = "<div style=\"color: red;\">$profitandloss</div>"; // notice echo here
}
elseif ($profitandloss>$zero) { // you had missed this brace?
$green_div = "<div style=\"color: green;\">$profitandloss</div>";
}
then you need to output the two divs in the correct table locations.
Maybe this is what you're looking for - inserting simple condition in a row:
echo "<tr style=\"color: ".($profitandloss > $zero ? 'green' : 'red')."\"><td>" . $row['trade_id'] . "</td><td>" . (...)
or just save your result to variable and use it in the row:
if($profitandloss<$zero) {
$profitText = "<div style=\"color: red;\">$profitandloss</div>";
} elseif ($profitandloss>$zero) {
$profitText = "<div style=\"color: green;\">$profitandloss</div>";
}
in your row place it like that:
echo '(...) <td>Profit: '.$profitText.'</td> (...)';
you need to use echo
if($profitandloss<$zero){
echo "<div style=\"color: red;\">$profitandloss</div>";
} elseif ($profitandloss>$zero)
echo "<div style=\"color: green;\">$profitandloss</div>";
}
Related
I'd like to add an active class with PHP instead of using javascript.
But I probably broke the code somewhere.
I'm using bootstrap.js version 3 and I'm building my tabs using PHP because I need to use my tabs on the control panel in my app.
So essentially I didn't add any javascript or jquery so far, I'm just using the native functions for tabs in bootstrap.js (and jquery):
<?php foreach ($database as $item): ?>
<?php
if (isset($item['associations'])) {
foreach ($item['associations'] as $value) {
echo "<div class='modal' id='des_" . $value['des_id'] . "'>";
echo "<div class='modal-sandbox'></div>";
echo "<div class='modal-box'>";
echo "<div class='modal-body'>";
echo "<div class='close-modal'>✖</div>";
echo "<h3>Credit and values</h3>";
echo "<h5>" . $value["designation_name"] . " - Credit: " . $value["designation_credits"] . "</h5>";
echo "<ul class='nav nav-pills'>";
if (isset($value['credits']) && !is_string($value['credits'])) {
foreach ($value['credits'] as $credits) {
$first_element = reset($value["credits"]);
if (isset($value['credits'])) {
if ($first_element[4] == $credits[4]) {
echo "<li class='active'><a href='tab_" . $credits[4] . "' data-toggle='tab'>Identifier: " . $credits[0] . "</a></li>";
} else {
echo "<li><a href='tab_" . $credits[4] . "' data-toggle='tab'>Identifier: " . $credits[0] . "</a></li>";
}
}
}
}
echo "</ul>";
echo "<div class='tab-content clearfix'>";
if (isset($value['credits']) && !is_string($value['credits'])) {
foreach ($value['credits'] as $credits) {
if (isset($value['credits'])) {
if ($first_element[4] == $credits[4]) {
echo "<div class='tab-pane active' id='tab_" . $credits[4] . "'>";
} else {
echo "<div class='tab-pane' id='tab_" . $credits[4] . "'>";
}
echo "<div class='single-credit'>";
?>
<?=form_open();?>
<?php
echo "<input type='hidden' name='id' value='" . $credits[4] . "'>";
echo "<div>Identifier: <span>" . $credits[0] . "</span></div>";
echo "<input name='edit_credits' type='submit' value='Edit' class='submit'>";
echo "</div>";
echo "</div>";
echo "</div>";
?>
<?=form_close()?>
<?php
}
}
}
echo "</div>";
echo "</div>";
echo "</div>";
echo "</div>";
}
}
?>
<?php endforeach;?>
To explain my code I will start from the list:
Since it's a really complex array, I'm taking the first element with the reset function to add the active class only on that very element.
Then I added the href element to point to the id of the matching div later on, and then I close my list.
Later on, I create another loop and I use the id to match both the list and the div and I used the trick that I used before to add the class active only on my first element of my array.
Everything works except for the fact that, if I click another element of the list, the matching div won't work, the very first element of the array will keep the class active, so I can't switch beteween element.
I need to spot the error.
UPDATE:
The problem that I have with jquery, in this case, is that if I use a toogleClass for example like that:
$(".nav-pills li").click(function(e) {
e.preventDefault();
tab = $(this).find('a').attr('href');
$("#" + tab).toggleClass('active');
});
It won't be able to toggle properly the class on the first element, that one to which I added the class active with php.
I think you miss to change the URL when click it, in the backend (PHP) you need to know where the user is, so, you check the "parameter" and mark active or not the current tab.
For example:
printf("<div class='tab-pane %s' id='...'>", $credits[4] == $_GET['tab'] ? 'active':'');
<?php
foreach($catalog as $id=> $item) {
echo get_item_html($id,$item);
}
// here is the functions page code :
function get_item_html($id,$item){
$output = "<li><a href='#'><img src='"
. $item["img"] . "' alt='"
. $item["title"] . "' />"
. "<p>View Details</p>"
. "</a></li>";
return $output;
}
?>
I have used it for displaying product categories i checked several time but I did find any error but it display undefined variable
please help me to find out this solution
I'm creating a PHP web application in combination with SQL to order stuff. I'm randomly generating textboxes based on the amount of records I have. I want to pass the individual values to insert to the database. I want each cell to have a textbox in which I can input values and pass them with that cell. So if I want to order 2 of cell one I want to pass that value into a database.
Here's the code:
$i = 0;
if ($AanbodsTabel === false){
die(mysql_error());
}
while ($row = mysqli_fetch_array($AanbodsTabel)){
echo "<tr><td width = 4%>". htmlentities($row['Fustcode']) . "</td>";
echo "<td width = 8%>" . htmlentities($row['cultivar']) . "</td>";
echo "<td width = 6%>" . htmlentities($row['AantalFust']) . "</td>";
echo "<td width = 6%>" . htmlentities($row['AantalPerFust']) . "</td>";
echo "<td width = 6%>" . htmlentities($row['AantalStelen']) . "</td>";
echo "<td width = 4%><input type ='text' id='Bestelbox' value='' name='AantalBestellen".$i."'></td>";
echo "<td width = 6%>" . htmlentities($row['AantalFustBesteld']) . "</td>";
echo "<td width = 6%>" . htmlentities($row['AantalStelenBesteld']) . "</td>";
135: echo '<td width = 8%><a href="Home.php?aanbodid='. htmlentities($row['aanbodid']) .'&hoeveelheid='. $_GET["AantalBestellen$i"]. '"'
. ' class = "btnBestel">Bestel</a></td></tr>';
$i = $i + 1;
}
The error I'm getting is undefined index. I guess this is because the name isn't unique. But whatever I do I still get the error.
I'm getting:
undefined index: AantalBestellen0 on line 135;
you can use javascript for example
note i will bold the code you need, the rest is context.
note this is from a similar project of mine but you should get the point.
<div id="meldingen_top_div">
<?php
echo "<div class='alert_Top_Div_Left_Outer'>
<input type='submit' value='Openstaande Meldingen' class='btnLoad' name='' onclick='toggleThis(" . '"' . "#meldTableTop" . '"' . "), toggleThis(".'"'.".alert_Top_Div_Left_Inner".'"'.")'/>
<div class='alert_Top_Div_Left_Inner'>
<table class='tableRed' id='meldTableTop'><caption/><thead/><tfoot/><tbody>";
$sqlVII = "SELECT Meldingen.Melding_id, FORMAT(Meldingen.Melding_datum, 'dd-mM-yyyy') AS Melding_datum, Producten.Product_naam, Leveranciers.Leverancier_naam, Meldingen.Melding_notitie
FROM MovieWorld.dbo.Producten,
MovieWorld.dbo.Meldingen,
MovieWorld.dbo.Categorieen,
MovieWorld.dbo.Leveranciers
WHERE Producten.Product_id = Meldingen.Product_id AND Producten.Categorie_id = Categorieen.Categorie_id AND Categorieen.Leverancier_id = Leveranciers.Leverancier_id AND Producten.Product_actief = 1 AND Meldingen.Melding_actief = 1
";
$resV= $db->executeQuery($sqlVII);
$i=1;
if($resV!=FALSE){
while($arrx = sqlsrv_fetch_array($resV)){
Code wich is relevant for you:
echo "<tr>
<td><p class='meldDate'>{$arrx['Melding_datum']}</p></td>
<td>{$arrx['Product_naam']}</td>
<td>{$arrx['Leverancier_naam']}</td>
<td><input id='txt{$i} type='text' name='thisdoesntevenmatter' value='..'/></td>
<td><img src='somerandompath.png' onclick='getTxt(".'"txt{$i}"'.")'/></td>
</tr>"
$i=$i+1;
}
}else{}echo '</tbody></table></div></div>';
?>
</div>
and the Javascript function
function getTxt(id){
var tempId = document.getElementById(id).value;
return tempId;
}
however.. you can also send the value back into a php variable, OR use AJAX to pass it to an PHP class to insert it into the Database.
edit
Your quotes in line 135: seem incorrect. try this:
echo '<td width = 8%><a href="Home.php?aanbodid={htmlentities($row['aanbodid']) }&hoeveelheid={$_GET['AantalBestellen$i']}"'
. ' class = "btnBestel">Bestel</a></td></tr>';
Where you get undefined index? on that $_POST['AantalBestellen'] ?
Use
if (isset($_POST['AantalBestellen'])) { }
to identify that the post value has been set
same do later for the hoeveelheid:
if (isset($_GET['hoeveelheid'])) { }
echo-ing the input field:
echo "<td width = 4%><input type ='text' id='Bestelbox' value='" . $_GET['hoeveelheid'] . "' name='AantalBestellen'></td>";
Also, dont forget to clear the spaces in:
'&hoeveelheid = '. $_POST //near the =
I reading results from DB and i would like highlight (change color) by different value:
my reading function
{
...
...
echo "<td>" . $row['value1'] . "|</td>";
echo "<td>" . $row['value2'] . "|</td>";
}
and if value1 = 1 then all results in row is red if value1 =2 then this row is green etc?
it's posible to do with php or just javascript/jquery?
It's always better to use class attribute, because at this time you may want just change background color, but later also change color, font-size or anything...
my reading function
{
...
...
echo "<td class='custom-value1-{$row['value1']}'>" . $row['value1'] . "|</td>";
echo "<td class='custom-value2-{$row['value2']}'>" . $row['value2'] . "|</td>";
}
and css:
.custom-value1-1 {
background: lightblue;
...
}
.custom-value1-2 {
background: darkblue;
...
}
this way you make your work easier and clearer.
Yes you can do it like this:
my reading function //Change this to your loop or condition
{
...
...
$style = '';
switch($row['value1']){
case 1:
$style = 'background-color:#FF0000';
break;
case 2:
$style = 'background-color:#00FF00';
break;
}
echo "<tr style='". $style ."'>";
echo "<td>" . $row['value1'] . "|</td>";
echo "<td>" . $row['value2'] . "|</td>";
...
echo "</tr>";
}
So I have a script that auto-generates ID's based on a column in a database. like such:
echo "<tr class='task' id='task-" . $row['task_id'] . "'>";
And then I have a script that is below that I am wanting it to change each tr class name based on some information from another column. I tried placing the same code 'task-". $row[task_id] ."' into the document.getElementById field but it didn't resolve in the browser. resolve meaning it stayed as 'task-" . $row[task_id] . "' instead of changing $row[task_id] into a number. I need to make that change to a number so it matches the id's of the tr.
var resolved = <?php echo $_SESSION['resolved']; ?>;
if (resolved == 1)
{
document.getElementById('task-" . $row[task_id] . "').className ="task resolved";
}
else
{
document.getElementById('task-" . $row[task_id] . "').className =" task";
}
You need to echo out the variables like so:
var resolved = <?php echo $_SESSION['resolved']; ?>;
if (resolved == 1)
{
document.getElementById('task-<?php echo $row['task_id'] ?>').className ="task resolved";
}
else
{
document.getElementById('task-<?php echo $row['task_id'] ?>').className =" task";
}
But it's better to not do it in javascript but to just do it in your PHP like so:
echo "<tr class='task " . ($_SESSION['resolved'] == 1 ? 'resolved' : '') ."' id='task-" . $row['task_id'] . "'>";
that adds the resolved class to as you are generating your HTML. However you will probably need a better way to test if a task is resolved because with the $_SESSION['resolved'] == 1 it's going to mark ALL tasks as resolved. So assuming your row has a column like `$row['resolved'] you could do:
echo "<tr class='task " . ($row['resolved'] == 1 ? 'resolved' : '') . "' id='task-$row[task_id]'>";
No need for javascript, if your status is in a column of that table....
if($row['resolved']=="1){
$class="task resolved";
} else {$class = "task";}
echo "<tr class='".$class."' id='task-" . $row['task_id'] . "'>";
Or a much simpler version...
echo "<tr class='task ".($row['resolved'] == 1 ? 'resolved' : '')."' id='task-".$row['task_id']."'>";