this is my PHP code:
$query="SELECT name, surname, address FROM employee";
$results = mysql_query($query);
while ($row = mysql_fetch_assoc($results)) {
echo '<tr>';
foreach($row as $field) {
echo '<td class="element">' . htmlspecialchars($field) . '</td>';
}
echo '</tr>';
And this is my CSS:
.element{
color:red;
}
But fields displayed in my HTML page aren't red. Why it doesn't work?
I already tried with
echo "<td class='element'>" . htmlspecialchars($field) . "</td>";
and
echo '<td class=\"element\">' . htmlspecialchars($field) . "</td>";
but with no success.
I suggest to specify element in your css.
td.element{
color:red;
}
Also it's a good idea to give your "tr" an ID
"tr id="myTR"...
#myTR td.element{
color:red;
}
then try this.
echo '<td style="color:red">' . htmlspecialchars($field) . '</td>';
even this 1 below shold work
echo '<td class="element">' . htmlspecialchars($field) . '</td>'; // for this just dont remove your css class
you just copy and paste to replace your code. and remove that class in css
the issue of mysl_ does not affect that not to display, i am using latest php engine and they work fine, even though we need to change.
Related
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>";
}
In CodeIgniter, I'm trying to create a table which fills with data from the Facebook Graph API.
The JSON loads a controller which passes data to a view, and it is this view which is added to a pre-existing table.
My PHP view looks like this:
if (array_key_exists('is_community_page', $json)==FALSE){
echo '<tr>';
echo '<td>ID</td>';
echo '<td><a href="' . $json['link'] . '">'. $json['name'] .'</td>';
if (!empty($json['website'])) {
if (!preg_match("~^(?:f|ht)tps?://~i", $json['website'])) {
$json['website'] = "http://" . $json['website'];
}
echo '<td>' . $json['website'] . '</td>';
}
else {
echo '<td>N/A</td>';
}
if (!empty($json['likes'])) {
echo '<td class="num">' . number_format($json['likes']) . '</td>';
}
if (!empty($json['checkins'])) {
echo '<td class="num">' . number_format($json['checkins']) . '</td>';
}
echo '</tr>';
}
And the jQuery/JSON looks like this:
$.ajax({
url: "<?php echo site_url('controller/function'); ?>",
type: 'POST',
data: form_data,
success: function(data) {
$('#results_table').html(data);
}
});
But when the data is returned, it just inserts the <a> elements between the <table> tags, and none of the <td> or <tr>.
Can anyone see why it might be ignoring the table row and table data tags, yet still keeping anchor tags and all the desired content?
Have you tried print_r($json) before the if to see if you're getting any output whatsoever from the Facebook data?
As per the comments, adding <tbody> to the table seemed to fix this adequately.
Perhaps try appending markup to a string and return the final value?
$row_data = '';
if (array_key_exists('is_community_page', $json)==FALSE){
$row_data .= '<tr>';
$row_data .= '<td>ID</td>';
$row_data .= '<td><a href="' . $json['link'] . '">'. $json['name'] .'</td>';
if (!empty($json['website'])) {
if (!preg_match("~^(?:f|ht)tps?://~i", $json['website'])) {
$json['website'] = "http://" . $json['website'];
}
$row_data .= '<td>' . $json['website'] . '</td>';
} else {
$row_data .= '<td>N/A</td>';
}
if (!empty($json['likes'])) {
$row_data .= '<td class="num">' . number_format($json['likes']) . '</td>';
}
if (!empty($json['checkins'])) {
$row_data .= '<td class="num">' . number_format($json['checkins']) . '</td>';
}
$row_data .= '</tr>';
return $row_data;
}
In the loops below, PHP doesn't seem to pass the outputted string back out to the loop that contains it for further manipulation, then making the output from this loop nothing at all.
Please help me understand why this is...
<?php
$finalTablePrint = '<html><body style="font-family:Tahoma, Verdana, Geneva, Arial, Helvetica, sans-serif; font-size:40px;">
LTU Activity Report<br/>
<span style="font-size:18px;"> Date Created: ' . date("l jS \of F Y") . '<br/><br/>
<table width=100% style="background-color:white; font-size:12px; border-collapse:collapse;" >
<tr style="font-size:12px; font-weight:600; background-color:#d2dbdf; height:35px;">
<td>Activity</td><td>Department</td><td>Hours Spent</td><td>Month</td>
</tr>';
while ($row = mysql_fetch_assoc($result)) {
$finalTablePrint .= '<tr><td colspan=4>' . $row['activity'] . '</td></tr>';
while ($row_1 = mysql_fetch_assoc($result)) {
if ($row_1['activity'] == $row['activity'] && $row_1['department'] == $row['department']) {
$finalTablePrint .= '<tr style="height:30px;"><td colspan=3>' . $row['department'] . '</td>' . '<td>' . $row['hours'] . '</td><td>' . $row['month'] . '</td></tr>';
}
}
}
echo $finalTablePrint .='</table><script type="text/javascript">javascript:window.print(); setTimeout(\'window.location="../../admin/index.php"\',"1000");</script></body></html>';
Note that when you set off your second loop, you are calling the same SQL statement as the original one before it
while ($row = mysql_fetch_assoc($result)) { //1st while
while ($row_1 = mysql_fetch_assoc($result)) { //2nd while
which in turn means that this if statement will allways be true....
if ($row_1['activity'] == $row['activity'] && $row_1['department'] == $row['department']) {
and this will allways echo:
$finalTablePrint .= '<tr style="height:30px;"><td colspan=3>'.$row['department'].'</td>'.'<td>'.$row['hours'].'</td><td>'.$row['month'].'</td></tr>';
so you should really remove your 2nd while/if statement... since it achieves nothing at the moment
Because js script at the end navigates you away too quickly you have no chance to see what happened! Remove it until you fix things the way you like it, and make sure that js is doing exactly what you are asking for.
I want to query a result from a table. The problem I run into is if the text is very long it will not auto-wrap to another line"best way i can think to explain it". It displays the text on one line and will go way off to the side of the page if the text is long. How can I get the text to space correctly. Thanks
here is my code:
<?php
echo "<div class=\"item_list2\">";
$get_items = "SELECT * FROM items WHERE category='Art'";
$result = mysql_query($get_items);
//loop! loops through the multiple results of the $get_items query
while($item = mysql_fetch_array($result, MYSQL_ASSOC)){
echo "<b>" . $item['item'] . "</b><br/>" . $item['email'] . "<br/>";
echo $item['price'] . "</b><br/>" . $item['category'] . "</b><br/>" . $item['extra'];
echo "<br/><a href='manage.php?delete=" . $item['id'] . "'><small>[Delete]</small></a><br/><br/>";
}
echo "</div>";
?>
Just use CSS to break it.
.container
{
word-wrap: break-word;
}
maybe you want to add..
nl2br($item['extra']);