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']."'>";
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':'');
I am trying to use jQuery to change the background color of the select element based on the option chosen within.
To generate the select elements, I'm using the following code:
while ($qrow = $qquery->fetch(PDO::FETCH_ASSOC)) {
$aqanswer = "qAnswer" . $qrow['questionID'] . "";
$aqcomments = "qComments" . $qrow['questionID'] . "";
echo "<tr><td class='auditf'>" . $qrow['qDesc'] . "</td><td class='auditm'><select name='qAnswer" . $qrow['questionID'] . "' form='entryform' id = 'selectqAnswer". $qrow['questionID'] ."'>";
if (isset($_SESSION[$aqanswer])) {
if ($_SESSION[$aqanswer] === 'Green') {
echo "<option value='Green' selected='true' class = 'green'>Green</option>";
echo "<option value='Red' class='red'>Red</option>";
} elseif ($_SESSION[$aqanswer] === 'Red') {
echo "<option value='Green' class ='green'>Green</option>";
echo "<option value='Red' selected='true' class = 'red'>Red</option>";
}
unset($_SESSION[$aqanswer]);
} else {
echo "<option value='Green' selected='true' class = 'green'>Green</option>";
echo "<option value='Red' class = 'red'>Red</option>";
}
echo "</select></td><td class='auditl'><textarea name='qComments" . $qrow['questionID'] . "' rows='3' cols='30' maxlength='255' value='N/A'>";
if (isset($_SESSION[$aqcomments])) {
echo $_SESSION[$aqcomments];
unset($_SESSION[$aqcomments]);
} else {
}
echo "</textarea></td></tr>";
}
And my jQuery:
$("[id^=selectqAnswer]").change(function () {
var color = $("option:selected", this).attr("class");
$("[id^=selectqAnswer]").attr("class", color);
});
Now this does work, but it causes every single box on the page to change, obviously. The problem is, the number of boxes on the page can change based on user options, so I don't have a fixed list of select IDs that I could reference individually in my jQuery. I've tried Googling the issue, but every single result talks about specifically naming the ID, not working with an ID that's generated procedurally. How do I only change a specific select element's background color based on the select option selected when I don't know what the ID will specifically be ahead of time.
$("[id^=selectqAnswer]").attr("class", color);
this will change the color for every #selectqAnswer select, if you want to change the color of the current select use this
Javascript
$(this).attr("class", color);
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'm running a script that fetches a row from a MySQL table and is supposed to then pass certain variables from that row to the next page to be used in a form that allows user updating.
Here's the excerpt from the script that is trying to pass the variables to the next page:
if ($row['home_score'] == '0' && $row['away_score'] == '0') {
echo '<td><img src="images/report_icon.png" alt="Report Score" /></td>';
}
If I omit everything after "&game_id=" in the href, it displays fine. However, once I start adding the variables, it cuts off the function and stops displaying the page.
Am I doing something simple wrong with the syntax? I've tried playing around with different ways to write it, but to no avail. Do I need to utilize a http_build_query() to make this work?
Here's the entire script code if you need more info:
<?php
// Connect to the database:
require ('../mysqli_connect.php');
// Make the query for games from the schedule database and determine the game location:
$q = "SELECT tl.game_date, tl.game_time, tl.away_team, tl.home_team, tl.home_score,tl.away_score, tl.arbiter_id, us.football_location, us.football_map
FROM test_league tl
INNER JOIN user_schools us ON (tl.home_team = us.school_name)
ORDER BY tl.game_id";
$r = mysqli_query($db, $q);
// Declare two variables to help determine the background color of each row:
$i = 0;
$bgcolor = array('row1', 'row2');
// Begin function to print each game as a row:
while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
echo '<tr class="' . $bgcolor[$i++ % 2] .'"><td>' . $row['game_date'] . '</td><td>' . $row['game_time'] . '</td><td class="alignleft">' . $row['away_team'] . ' vs<br>' . $row['home_team'] . '</td>';
// Determine if the score has been reported:
if ($row['home_score'] == '0' && $row['away_score'] == '0') {
echo '<td><img src="images/report_icon.png" alt="Report Score" /></td>';
} else {
echo '<td>' . $row['home_score'] . '<br>' . $row['away_score'] . '</td>';
}
echo '<td>' . $row['football_location'] . '</td><td>' . $row['arbiter_id'] . '</td></tr>';
}
mysqli_free_result ($r);
mysqli_close($db);
?>
Any and all advice is greatly appreciated!
Try this, you've got your speech marks and dots mixed up :)
if ($row['home_score'] == '0' && $row['away_score'] == '0') {
echo '<td><img src="images/report_icon.png" alt="Report Score" /></td>';
}
Hope this helps!
$url = 'www.example.com?' . http_build_query($row,'','&');
Well if the status is 1 it's "Active Insurance Event" and if 2 it's "Completed Insurance Event".
if(!empty($ins_event))
{
echo "<tr><td> <img src='/check-icon.gif'> <a href='". matry::here(array('event_id'=>$ins_event['id'])) . "'>" . ( $ins_event['status'] == 2 ? "Completed Insurance Event": "Active Insurance Event") . "</a></td></tr>";
}
else
{
echo "<tr><td>" . cbox_return() . "<a href='". matry::here_to('new', array('tfilt'=>'IN', 'pfilt'=>$patient->code)) . "' style='color: #000; color:$rx_image_color'>**Ins Event Not Created**</td></tr>";
}
I have a variable for color here:
<?php
$rx_event_colors = $rx_ev_status = '#009933';
?>
How can i use this variable to grab the status of 2 and change the font color.
Should i break up the script and use if {} else {} statements?
updated code:
echo "<tr><td> <img src='/check-icon.gif'> Completed Insurance Event' : '>Active Insurance Event') . "</td></tr>";
You would basically do the same kind of ternary operation:
($ins_event['status'] == 2 ? ' style="color: ' . $rx_ev_status . '"' : '')
Is this what you mean? Your question isnt that clear..
if(!empty($ins_event))
{
echo "<tr><td> <img src='/check-icon.gif'> <a ".($ins_event['status'] == 2 ? 'style="color:'.$rx_ev_status.';"': '')." href='". matry::here(array('event_id'=>$ins_event['id'])) . "'>" . ( $ins_event['status'] == 2 ? "Completed Insurance Event": "Active Insurance Event") . "</a></td></tr>";
}
Here is an edit with the echo broken out onto different lines for clarity...
echo '<tr><td> <img src="/check-icon.gif">';
echo '<a '.($ins_event['status'] == 2 ? 'style="color:'.$rx_ev_status.';"': '')." ";
echo ' href="'. matry::here(array('event_id'=>$ins_event['id'])) . '">';
echo ($ins_event['status'] == 2 ? 'Completed Insurance Event': 'Active Insurance Event');
echo '</a></td></tr>';
Yes, for performance and code maintenance reasons logic operations should be done as infrequently as possible. Set both the text status and color variables inside of a single if { … } else { … } block. After storing the status and colors to variables, you can simplify the echo statement greatly.
echo "<tr><td> <img src='/check-icon.gif'> <a href='". matry::here(array('event_id'=>$ins_event['id'])) . "'>$status</a></td></tr>";
I would also consider storing the output of matry::here(…) in a variable for even easier code reading.