Hiding a description in a dynamic table - php

I have a table that is created using PHP and when you click on the name of the item I want the description to show.
<table border="1" bordercolor="#000000">
<tr class="trintial">
<th width="50px"><input type="checkbox" readonly="readonly" checked="checked" /></th>
<th width="300px">To-Do Item</th>
<th width="100px">Priority</th>
<th width="100px">Due Date</th>
</tr>
<?php
$i=0;
while ($i < $num)
{
$entrynum=mysql_result($result,$i,'todo.entry');
$f2=mysql_result($result,$i,'todo.item');
$f3=mysql_result($result,$i,'todo.priority');
$f4=mysql_result($result,$i,'todo.duedate');
$f5=mysql_result($result,$i,'todo.description');
?>
<tr id="<?php echo "row$entrynum"; ?>" class="trintial">
<td><input type="checkbox" onchange="change(this,<?php echo "row$entrynum"; ?>)" name="<?php echo "box$entrynum"; ?>" value="checked" /></td>
<td><?php echo $f2; ?></td>
<td><?php echo $f3; ?></td>
<td><?php echo $f4; ?></td>
</tr>
<?php
$i++;
}
mysql_close($con);
?>
</table>

i need it to ouput when the text of $f2 is clicked
I like this view of the world. You really can't do that. Because what you don't echo with PHP, the user never receives in his browser. (Remember: PHP is executed server-side only, HTML, CSS, JavaScript are "executed" in the users browser.)
To make your description appear when $f2 is clicked, you'll need to pack it into your html and then hide it with CSS. After that you'll need to write some smart JavaScript which overwrites the CSS of your description, when $f2 is clicked. That's not trivial, but it's doable. If you need some help with it, post exactly what you understand under "show"ing the description, because there are lots of ways to do this.

Supertech, I get the impression that you are confusing server-side code (PHP in this case) with client-side code. PHP doesn't run in the user's browser. Once the page data has been sent, PHP is no longer running. What you want to do is create some Javascript to modify the style (CSS) of the elements on the page.
Start by outputting your table as it would look with everything expanded. Once this is done, add a CSS class to all of the elements, such as "hidden". Then in your CSS for the page, define this class with something like this
.hidden {
display:none;
}
Now, you need to add the appropriate Javascript to links to display. Something like this should do
Click Me
If all is well, yourelement will now be displayed.
You can read up on the CSS display property here:
http://www.w3schools.com/css/pr_class_display.asp
And, the Javascript necessary to change the style here:
http://www.randomsnippets.com/2008/02/12/how-to-hide-and-show-your-div/
I hope this helps you understand how all of these essentially unrelated pieces fit together in your web application.

Related

Put text between autofilled PHP-Table

I'm currently working on a table that contains 'Name', 'Info', 'Price' and 'Duration'. However the PHP-Script is connected to a database.
I want to autofill the tablecells: 'Name', 'Price', 'Duration' via the Database by using PHP and SQL and that works perfectly fine for me. Though, I want to customize the content that's in the individual Info cells (e.g. Readmore jQuery and redirect to other pages).
I tried a little bit with using tags inside the Database and other weird stuff which, obviously, didn't work.
Is there a more elegant way of solving my problem than setting up a complete normal table without PHP/SQL, where I'd have to put in every bit of data about Name,Price and Duration manually?
<table class="table table-bordered table-hover tablesorter row sortable">
<thead>
<tr>
<th class="header">Name</th>
<th class="hidden-xs">Info</th>
<th>Price (in Euro)</th>
<th>Duration</th>
</tr>
</thead>
<tbody>
<?php
//Connect to Database
$db=mysql_connect ("xxxx", "xxxx", "xxxx") or die ('Oops! Da hat wohl' . mysqli_error(). 'Mist gebaut');
//Choose Database
$mydb=mysql_select_db("massageke_de");
//Query Database
$sql="SELECT * FROM Angebote";
//-run the query against the mysql query function
$result=mysql_query($sql);
//Show Results
while($row=mysql_fetch_array($result)){
//Start table ?>
<tr>
<td class="col-sm-2"><?php echo $Name =$row['Name'] ; ?></td>
<!--In this <td>-tag I want to put long textes with links-->
<td class="hidden-xs col-sm-5">echo $Name =$row['Info'];?<</td>
<td class="col-sm-2"><?php echo $Preis =$row['Preis']; ?></td>
<td ckass="col-sm-1"><?php echo $Dauer =$row['Dauer']; ?></td>
</tr>
<?php } ?>
</tbody>
</table>
Thanks in advance for helping.
Don't bother asking me additional questions.
P.S.: I hope you can help, without needing the CSS of Bootstrap, that I used.
P.P.S.:I know that my PHP-Script is not protected against PHP-Injection
(but I want to and will learn how to secure it)
Edit: As Jester asked for it. I made it quickly with photoshop because I think an image can express much better, what I want to achieve, than my poorly coding-skills.
Get to the image
Seems to me like the easiest way would be to just edit the info column in the database for each? If you want to do it in php i'd suggest making an array using the names (ids would be better but it seems you don't have access to those?) as keys:
$info['Aroma Teilkoerper'] = "Text about aroma teilkoerper";
$info['Aroma Ganzkoerper'] = "Text about aroma ganzkoerper";
and so on until you have all. Then in the loop:
//Show Results
while($row=mysql_fetch_array($result)){
//Start table ?>
<tr>
<td class="col-sm-2"><?php echo $Name =$row['Name'] ; ?></td>
<!--In this <td>-tag I want to put long textes with links-->
<td class="hidden-xs col-sm-5">echo $Name =$info[$row['Name']];?></td>
<td class="col-sm-2"><?php echo $Preis =$row['Preis']; ?></td>
<td ckass="col-sm-1"><?php echo $Dauer =$row['Dauer']; ?></td>
</tr>
<?php } ?>
Hoping that is a workable solution for you? (also you had a syntax error in your closing php tag.
echo $Name =$row['Info'];?< // <----- should be ?> of course

Getting contenteditable div's current value and update database

I'm new to web development and I'm trying to create editable database interface. I have got checkboxes in every row and an edit button. I need to update table(in database) when button clicked(rows that are checked). However I can't get current values in cells. I tried contentedittable div to display attiributes.
<?php while ( $rows = mysql_fetch_array($result)): ?>
<td align="center" bgcolor="#FFFFFF"><input name="need_delete[<?php echo $rows['UniqueID']; ?>]" type="checkbox" id="checkbox[<?php echo $rows['UniqueID']; ?>]" value="<?php echo $rows['UniqueID']; ?>"></td>
<td bgcolor="#FFFFFF"><div contenteditable><?php echo $rows['Timestamp']; ?></div></td>
<td bgcolor="#FFFFFF"><div contenteditable><?php echo $rows['Name']; ?></div></td>
<td bgcolor="#FFFFFF"><div contenteditable><?php echo $rows['Email']; ?></div></td>
<?php endwhile; ?>
I also tried to give unique names to divs, but it didn't work. I want to get current values from there if possible.
<?php
if( ! empty($_POST['edit']) ){
$query = 'UPDATE `asd` SET `Timestamp` = '????' WHERE `UniqueID`='.(int)$id;
mysql_query($query);
}
?>
Checkboxes work fine when I delete rows from table. Any suggestions ?
Thanks.
There are several javascript libraries that help with this problem:
x-editable http://vitalets.github.io/x-editable/ (bootstrap)
datatables https://datatables.net/ (jquery)
I had a similar issue that I solved here:
http://www.abrandao.com/2019/12/saving-contenteditable-html-using-php/
basically, it involves reading the HTML and using PHP parser to exctract the tag and the update the data

Correct syntax using JS 'onclick' functions inside PHP echo statements AND with embedded XML fetching

I am likely to get b'*ch slapped because I haven't searched the forum enough before posting, but I really think I searched all relevant posts already, many seem not specifically covering the question I have, the others fly right over my beginner's head ( as I am new to PHP & js ). That being said...
I have built a PHP code to fetch data from an XML file using the $xml=simplexml_load_file();
No worries there, however one of the needed data 'entries' or 'fields' needs to exist within an onclick and/or an onmouseup javascript function.
The code is as follows:
<?php
$xml=simplexml_load_file('prod_test_feed_sameday.xml');
$max = 8;
$i = 1;
foreach($xml->product as $feed){
if ($i <= $max){
echo "<table id='{$feed->position}' class='prod_container'>
<td class='hidden_mask' id='{$feed->position}'>
</td>
<td class='hidden_image' id='{$feed->position}'><span style='background:url({$feed->prod_image_large}) no-repeat center;'/></span><div><a onmouseup='$('.hidden_image#{$feed->position}').slideToggle('slow');' onclick='$('.hidden_mask#{$feed->position}').hide();'><b>CLOSE</b></a></div>
</td>
<tr>
<td class='prod_image' id='{$feed->position}'>
<span style='background:url({$feed->prod_image_small}) no-repeat center; background-size:contain;'/></span>
</td>
<td rowspan='1' class='info_toggle' id='{$feed->position}'><a onmouseup='$('.hidden_image#{$feed->position}').slideToggle('slow');' onclick='$('.hidden_mask#{$feed->position}').show();><img src='images/zoom_02.png' title='See a larger image of these flowers' /></a>
</td>
</tr>
<tr>
<td colspan='2' class='prod_name' id='{$feed->position}'>{$feed->prod_name}
</td>
</tr>
<tr>
<td colspan='2' class='prod_price' id='{$feed->position}'><span id='{$feed->position}'>from: £{$feed->price}</span><a href='{$feed->prod_link}' target='_blank'><span class='buy_button'> Buy it now! </span></a></td>
</tr>
</table>";
$i++;
}
}
?>
The data and the CSS all work perfectly. There is a href link towards the end of the code, which also works perfectly.
I am racking my brain trying to find the error in my syntax within the onlick function.
I have tried all manners of backslashing, using trial and error, for exampel:
onclick='$(\'.hidden_mask#{$feed->position}\').hide();' or
onclick='\'$('.hidden_mask#{$feed->position}').hide();\'' or
onclick=\''$(\'.hidden_mask#{$feed->position}\').hide();\'' or even
onclick=\''$(\\'.hidden_mask#{$feed->position}\\').hide();\'' <--Freddy Krugar 'slashing' example
At any rate I am at a loss.
Try with double quotes and escape them:
echo " ...
onclick=\"$('.hidden_mask#{$feed->position}').hide();\"
...";
Or
echo " ...
onclick='$(\".hidden_mask#{$feed->position}\").hide();'
...";
DEMO
This way you do the escaping in the PHP only, without needing the Freddy Krugar escaping for the DOM parser.

Form Submit button doesnt work with PHP loop

I have this code
<tbody>
<form action="dsf.php" method="post">
<?PHP if(mysql_num_rows($leerdb) > 0) {while ($rs = mysql_fetch_row($leerdb)) {?>
<tr>
<td><input name="idecod[]" type="checkbox" value="<?php echo $rs[0]; ?>" /></td>
<td><?php echo $rs[0]; ?></td>
<td><?php echo $rs[1]; ?></td>
<td><?php echo $rs[2]; ?></td>
<td><?php echo $rs[3]; ?></td>
<td><?php echo $rs[4]; ?></td>
<td><?php echo $rs[5]; ?></td>
<td>BsF. <?php echo $rs[6]; ?></td>
</tr>
<?PHP }}?>
<input class="enviar" type="submit" name="enviar" id="enviar" value="Editar Asesor" />
</form>
</tbody>
I do not know why the submit button doesnt work. When I click on it, nothing happend.
It seems that something in the php loop is making the mess.. But as I know, PHP goes first than HTML, so, when FORM HTML comes, PHP code was already executed.
Where is my error.??
Thanks in advance.
Roberto
You are generating invalid HTML.
You can't have a form wrapped around table rows without wrapping it around the entire table.
You can't have a submit button placed between table rows.
The browser you are using is likely trying to recover from the error in such a way that the form is moved somewhere where it is allowed, but where it doesn't contain any of the controls.
NB: Different browsers recover from having forms in inappropriate parts of tables in different ways.
Use a validator, and write real HTML.
instead of opening the brackets for the while and if, use the php short tags. Example:
<?php if (condition): ?>
output your html here
<?php endif; ?>
Same thing goes for while. read about it here: www.php.net/manual/en/control-structures.while.php

Separating PHP code from HTML output

Very often I have heard people suggesting (and I have done it myself too a few times) to keep things separate: PHP code here, HTML there, external CSS, external JS and so on and so on.
Aside from the obvious readibility and maintenance advantages of doing this are there other strong advantages (e.g. in terms of server load or page processing time) in doing it?
As a trivial example, say we want to implement a table containing some products we read from a DB.
The output we want would be something like
<div class="description">This table lists all our products</div>
<table class="products">
<tr>
<th>Name</th>
<th>Available</th>
<th>Price</th>
</tr>
<tr>
<td>Prod 1</td>
<td>Yes</td>
<td>$100</td>
</tr>
...
...
</table>
<div class="info">Some generic info on the products here</div>
So here we have some static output (the 2 div elements and the table header) and some dynamic output (the actual table content).
We could leave all the static things out of PHP tags and try to keep PHP only where needed
<div class="description">This table lists all our products</div>
<table class="products">
<tr>
<th>Name</th>
<th>Available</th>
<th>Price</th>
</tr>
<?
for ($p=0; $p<count($products); $p++)
{
echo '<tr>';
echo '<td>'.$products[$p]["name"].'</td>';
echo '<td>'.$products[$p]["availability"].'</td>';
echo '<td>'.$products[$p]["price"].'</td>';
echo '</tr>';
}
?>
</table>
<div>.....</div>
On the other hand we could embed everything in PHP
<?
echo '<div class="description">This table lists all our products</div>';
echo '<table class="products"><tr><th>Name</th>'.
'<th>Available</th><th>Price</th></tr>';
for ($p=0; $p<count($products); $p++)
{
echo '<tr>';
echo '<td>'.$products[$p]["name"].'</td>';
echo '<td>'.$products[$p]["availability"].'</td>';
echo '<td>'.$products[$p]["price"].'</td>';
echo '</tr>';
}
echo '</table>';
echo '<div>.....</div>';
What are the reasons to choose one over the other?
The alternative syntax for control structures seems to be more readable to me:
<div class="description">This table lists all our products</div>
<table class="products">
<tr>
<th>Name</th>
<th>Available</th>
<th>Price</th>
</tr>
<?php foreach($products as $p): ?>
<tr>
<td><?php echo $p["name"]; ?></td>
<td><?php echo $p["availability"]; ?></td>
<td><?php echo $p["price"]; ?></td>
</tr>
<?php endforeach; ?>
</table>
<div class="info"><?php echo $info; ?></div>
If its just a piece of code for you to play with, it doesn't really matter at all.
But if an application grows more and more complex (and more people work in it), it will usually come to a point where it is vital to separate the view layer (here: HTML) from the code layer (here: PHP) - so you can assign designers to play around with the output and coders to play around with the functionality behind it.
This ain't a php-only topic, this is very general. Architectural models like MVC are based on similar theories.
I think that it is a very compact string <?= $var ?>
I do not suggest to use short sytax of php. Sometimes it can be problem to move code from one server to another.
Reason you need to do so is time. Nice code is simple to support and upgrade. In some cases it is performance issue also, but not in your case.
Ideal example in your case is:
You have to files
index.php
products.php
File products.php contain
<?php
...
foreach($products as $product)
{
$productHTML[] = '<tr><td>' . $product["name"] . '</td></tr>';
}
$productHTML = implode("", productHTML);
?>
index.php:
<html>
...
<?php echo $productsHTML;?>
...
</html>
Ofcourse more advence developers use more hard constructions, we use functions, class, template idea and etc. But such way is enough for small project.

Categories