get a hidden table to appear with with a click with php - php

Im creating a simple School grade system using only php and I'm stumped of trying to figure out how to get a hidden table to appear below the main table when click on a "durchschnitt number"
Below is the code for the main table
<?php
if (!isset ($_SESSION['Saved_contacts']))
$Kontakte = array (
array ("Hr.", "Fruehauf", "Dennis", "13.02.2002", "Brucknerweg 34", 5212, "Hausen", '<u>3.6</u>'),
array ("Fr.", "Kaufmann", "Katharina", "04.03.2002", "Neubertbogen 24", 1680, "Romont", "Durchschnitt"),
array ("Hr.", "Fiedler", "Marcel", "08.16.2002", "Via Stazione 98", 8143, "Stallikon", "Durchschinitt"),
array ("Hr.", "Oster", "Tim", "08.26.2002", "Via delle Vigne 98", 1773, "Vuaty", "Durchschinitt"),
array ("Fr.", "Eichelberger", "Tanja", "07.22.2002", "Semperweg 6", 4223, "Blauen", "Durchschinitt"));
else
$Kontakte = $_SESSION['Saved_contacts'];
?>
<div style="width: 80%; min-width: 550px">
<h2>Kontakt des Schülers ...</h2>
<table>
<tr> <th>Nr.</th> <th>Anrede</th> <th>Name</th> <th>Vorname</th> <th>Geburtsdatum</th> <th>Adresse</th> <th>PLZ</th> <th>Ort</th> <th>Durchschnitt</th> </tr>
<?php
for ($i=0; $i < count($Kontakte); $i++) {
echo "<tr> <td><em>".($i+1)."</em></td>" . "<td style='text-align: center'>".$Kontakte[$i][0]."</td>" .
"<td>".$Kontakte[$i][1]."</td>" . "<td>".$Kontakte[$i][2]."</td>" . "<td>".$Kontakte[$i][3]."</td>" .
"<td>".$Kontakte[$i][4]."</td>" . "<td>".$Kontakte[$i][5]."</td>" . "<td>".$Kontakte[$i][6]."</td>" . " <td>".$Kontakte[$i][7]."</td
" . " <td><</tr>";
}
?>
</table>
As you can see in the first aray on the last line i made a link so that i'm able to click it.
Below is the hidden table I want to hide and reappear
<div class="Note">
<div style="width: 80%; min-width: 550px">
<table class="grade_Fruehauf" style="">
<tr>
<th>Fruehauf</th>
</tr>
<tr>
<th>Deutsch</th>
<th>3.5</th>
</tr>
<tr>
<th>Math</th>
<th>3.5</th>
</tr>
<tr>
<th>Biologie</th>
<th>3.5</th>
</tr>
<tr>
<th>Französisch</th>
<th>4</th>
</tr>
<tr>
<th>Durchschnitt</th>
<th style="border-top:solid;">3.6<th>
</tr>
</table>
<div>
</div>
Appreciate your help :)

If you want to use PHP only then you can use session, on clicking link set a session variable in a different file and redirect back, check that session variable and show/hide w.r.t it. (though JavaScript is preferable)

It's better to use javascript.
You just need to add the onlick attribute on the "durchschnitt number" td like
<td onclick="show('tableid');"></td>
and specify an id to the div or the table (es.: <table class="grade_Fruehauf" style="" id="example">.
Then you need a javascript code like this:
<script>
function show(tableid){
var x=document.getElementById(tableid);
if (window.getComputedStyle(x).visibility === "hidden") {
x.style.visibility = "visible";
}else{
x.style.visibility = "hidden";
}
}
</script>

Related

PHP table, to add, remove and edit rows

I have issues with my table, I cannot use Javascript (which I know would be easier). I can only use HTML, PHP and CSS. This is my code that I currently have. The issues I need to resolve are the following:
I am able to add rows, delete and I am so also able to edit them by using the "contenteditable", however my issue is that every time I add a row or remove one, it refreshes the whole page. How can I fix this issue?
Also if there is a way to have an edit button instead of my conteneditable method.
Here is my code:
input {
display: block; /* makes one <input> per line */
width: 150px;
}
<?php
if( isset( $_REQUEST["btnadd"]) == "ADD") {
// add 1 to the row counter
if (isset($_REQUEST['count'])) $count = $_REQUEST['count'] + 1;
// set value for first load
else $count = 1;
}
if( isset( $_REQUEST["btnremove"]) == "REMOVE") {
// decrement the row counter
$count = $_REQUEST['count'] - 1;
// set minimum row number
if ($count < 1) $count = 1;
}
?>
<form name="form1">
<table class="table table-bordered table-striped table-hover text-center" align='center'>
<tr>
<th align="center">Name</th>
<th>Start </th>
<th>Size</th>
<th>First Condition</th>
<th>Second Conditon</th>
<th><input type="submit" name="btnadd" id="btnadd" value="ADD" align='center'></th>
</tr>
<?php
// print $count rows
for ($i=1; $i<=$count; $i++) {
echo ' <tr>
<td contenteditable="true"></td>
<td contenteditable="true"></td>
<td contenteditable="true"></td>
<td contenteditable="true"></td>
<td contenteditable="true"></td>
<td> <input type="submit" name="btnremove" id="btnremove" value="REMOVE"></td>
</tr>
';
}
?>
</table>
<input type="hidden" name="count" value="<?php echo $count; ?>">
</form>
every time I add a row or remove one, it refreshes the whole page. How can I fix this issue?
Without Javascript you can't.
if there is a way to have an edit button instead of my conteneditable method
It's rather hard to suggest what your code should be since you don't ofer a description of what the code is intended to do, other than the code which doesn't do what you want. I think you mean something like:
<?php
$width=5;
$data=isset($_REQUEST['data']) ? $_REQUEST['data'] : array();
$count=count($data);
if (isset($_REQUEST['delete'])) {
foreach ($_REQUEST['delete'] as $i) {
unset($data[$i]);
}
}
$data=array_values($data);
if( isset( $_REQUEST["btnadd"]) == "ADD") {
// add 1 to the row counter
$data[]=array();
}
...
foreach ($data as $i=>$row) {
print "<tr>\n";
for ($x=0; $x<$width; $x++) {
#print "<td><input type='text' name='data[$i][$x]'></td>\n";
}
print "<td><input type='checkbox' name='delete[]' value='$i'>";
print "</tr>\n";
}
print "<tr>
<td colspan='$width'>
<input type="submit" name="btnadd" id="btnadd" value="Add">
</td>
</tr>\n";

Sending items fetched by while loop from mysql database in email

Need some help.
I am trying to create a script that sends an html email to a shopper with a list of all the orderd items. I want the items fetched from the database to be stored in a single variable so that I can embed it in the message variable of mail() function. Or Just the Items fetched to be embeded in the email.
Have tried the code below but it only displays 1 item and not all that are in the loop. How can I do it?
I'll be grateful for your help.
$message3 = "
</td>
</tr>
<tr bgcolor='#A8CE58'><td><b>ITEMS ORDERED</b></td></tr>
<tr>
<td>
<table border='' style='margin-bottom: 10px; margin-top:10px; padding: 1px;border-right: : 1px solid #000;'>
<tr align='left'>
<th>---</th>
<th>Name</th>
<th>Qty</th>
<th>Total</th>
</tr>";
$ress= mysqli_query($server, "SELECT * FROM ordered_products WHERE session='$session'");
mysqli_data_seek($ress, 0);
while($colms=mysqli_fetch_array($ress, MYSQLI_NUM))
{
$message4 =
"
<tr align='left'>
<td><img src='e_images/$colms[8]' height='auto' width='50px'></td>
<td>$colms[2]</td>
<td >$colms[9]</td>
<td>ksh.$colms[5]</td>
</tr>";
}
$message = "$message3". $message4;
You need to concatenate the string
$message4 = '';
while($colms=mysqli_fetch_array($ress, MYSQLI_NUM)) {
$message4 .= //your html
}
What you doing now is overwrite the value of $message4 each time in the loop
With
.=
You solve this. Don't forget to declare the variable before the loop, else you will get a warning from PHP.

Let a div get a value from a fetch-array-table row after click on a cell with jquery

first I want to say that this question is related to jquery replacewith to get data with Ajax after click on a cell and if this against the rule I am sorry and delete this post.
I tried to get the value of a cell in a MySQL-generated table (with fetch_array). I want to click on a cell in the same and receive the value of the first cell in the row in a div-container.
Thanks to several entries here I found an example for "static" tables. Like this:
<table id="choose-address-table" class="ui-widget ui-widget-content">
<thead>
<tr class="ui-widget-header ">
<th>Name/Nr.</th>
<th>Street</th>
<th>Town</th>
<th>Postcode</th>
<th>Country</th>
<th>Options</th>
</tr>
</thead>
<tbody>
<tr>
<td class="nr"><span>50</span>
</td>
<td>Some Street 1</td>
<td>Glasgow</td>
<td>G0 0XX</td>
<td>United Kingdom</td>
<td class="use-address">
Use
</td>
</tr>
<tr>
<td id="chip" class="nr">49</td>
<td>Some Street 2</td>
<td>Glasgow</td>
<td>G0 0XX</td>
<td>United Kingdom</td>
<td>
<button type="button" class="use-address">Use</button>
</td>
</tr>
</tbody>
</table>
<br><br>
<div id="glasgow">Hello</div>
and
$("#chip").click(function () {
var scotland = $(this).closest("tr").find(".nr").text();
$("#glasgow").html(scotland);
});
http://jsfiddle.net/FnDvL/231/
Works fine. I insert this to my file, php, and get table data from mysql.
This is my code:
<html>
<head>
<script type="text/javascript" charset="utf-8" src="http://code.jquery.com/jquery-latest.min.js"></script>
<script>
$(".information").click(function () {
var rodney = $(this).closest("tr").find(".nr").text();
$(".clicked_info").html(rodney);
});
</script>
<link rel="stylesheet" type="text/css" href="css/style.css">
</head>
<body>
<?
$con = mysqli_connect('localhost','root','','boerse');
$sql="SELECT short, name FROM markets";
$result = mysqli_query($con,$sql);
?>
<div class="markets">
<?
echo "<table>
<thead border='0'>
<tr>
<th>Index</th>
<th>Name</th>
</tr>
</thead>
<tbody border='1'>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td class='nr'>" . $row['short'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td class='information'>Use</td>";
echo "</tr>";
}
echo "</tbody></table>";
?>
<div class="clicked_info">Hello</div>
</div>
<br><br>
</body>
</html>
Now my problem is that I can click on the td with class information, but div class 'clicked_info' doesn't change. But I did like in the fiddle. So what went wrong? Or where is the problem? It must have to do with the MySQL-matter. But why?
This way I want to check if I got the content from the cell and I can continue working with it (as seen in my post before).
Maybe anyone can help. And again I am sorry if I break rules here.
Thank you to everyone.
You need to wrap your code in document-ready handler
$(document).ready(function () {
$(".information").click(function () {
var rodney = $(this).closest("tr").find(".nr").text();
$(".clicked_info").html(rodney);
});
});
Currently when your script executes there is no $(".information") thus event is not binded properly.
OR
Move your script at the bottom of your page. like
<script>
$(".information").click(function () {
var rodney = $(this).closest("tr").find(".nr").text();
$(".clicked_info").html(rodney);
});
</script>
</body>
OR
You can use event-delegation
$(document).on("click", ".information",function () {
var rodney = $(this).closest("tr").find(".nr").text();
$(".clicked_info").html(rodney);
});

how to populate dropdown list values from mysql and show it inside the td of html table

I am trying to populate dropdown list values from mysql and show it inside the td of html table,i tried with below code but it not populating values from mysql can any help me how to do that.
<table id="CPH_GridView1" style="width:1452px">
<thead>
<tr>
<th style=" width:102px">Clien ID </th>
<th style=" width:100px">Country</th>
<th style=" width:248px">Network Name </th>
<th style="text-align:center; width:102px" >cppn </th>
</tr>
</thead>
<tbody>
<?php
$sql = mysql_query("SELECT * FROM clientpricenotifications");
while($rows=mysql_fetch_array($sql))
{
if($alt == 1)
{
echo '<tr class="alt">';
$alt = 0;
}
else
{
echo '<tr>';
$alt = 1;
}
echo '<td id="CPH_GridView1_clientid" style="width:140px" class="edit clientid '.$rows["id"].'">'.$rows["clientid"].'</td>
<td id="CPH_GridView1_country" style="width:160px" class="edit country '.$rows['id'].'">'.$rows["country"].'</td>
<td id="CPH_GridView1_networkname" style="width:156px" class="edit networkname '.$rows["id"].'">'.$rows["networkname"].'</td>';
?>
<td>
<select name=' . customer_name . '>
<?php
$query = 'SELECT cppn FROM clientpricenotifications';
$result = mysql_query($query, $db) or die(mysql_error($db));
while ($row = mysql_fetch_assoc($result))
{
echo '<option value="' . $row['id'] . '"> ' . $row['cppn'] . '</option>';
}
?>
</select>
</td>
</tr>'
}
?>
There seems to be a problem with this line:
<td> <select name='customer_name'>
Shouldn't it actually say either this:
<td> <select name="customer_name">
Or:
<td> <select name=' . customer_name . '>
And, that line is part of an echo statement that contains a string in single-quotes, but I can't see where the echo statement's closing single-quote is.
As a result, I think a large bulk of your output is being ignored by the browser because the tag is not being closed properly as some of the output is getting mangled. Check your output with View Source!
If your above code is complete, then I would guess that you're missing the connection to the MySQL server. See: http://www.php.net/manual/en/function.mysql-connect.php
For a related question with code sample, check the answer at: Create table with PHP and populate from MySQL
Not asked, but your table has non matching column widths defined in the styles: Clien(t) ID header 102px, while data cells are 140px.
Another place to look for is following line:
<td style="width:65px" class=" '.$rows["id"].'">
I would expect it should be the following:
<td style="width:65px" class="<?php echo $rows["id"] ?>">
As Vexen Crabtree mentioned, if you also check/post the html code of the HTML output, it would make it easier to diagnose the problem.

Unwanted table data with changing with JavaScript

I am making a table in which PHP is used in <td> tags. This contains a list of products and their prices. On the top of the table, I have inserted two buttons, one for arranging data in ascending order of price, and the other one is for descending order.
By default, the data is in ascending order. When I click on the descending order button, it works but with of the table data changes which is not required.
Here is my CSS code:
a {text-decoration:none; color:#0000FF;}
#hold .log {
color:#0000FF;
text-align:center;
font-size:20px;
}
h2 {color:#0000FF;}
#left { width:200px;border:1px;border-style:solid;margin:0 auto;margin-top:20px;text-align:center;border- color:#0000FF;float:left;}
#left div {border:1px;border-style:solid;border-color:#0000FF;}
#left div a {}
.style table {border:1px;border-style:solid;border-color:#0000FF;}
.style table tr td {border:1px;border-style:solid;border-color:#0000FF;color:#333;width:250px;}
.style table tr td input,select,textarea {width:250px;height:30px;}
Here is the code for the table:
<button onclick="as()">Ascending Order</button>
<button onclick="ds()">Descding Order</button>
<div class="style">
<table>
<tr>
<td>Title</td>
<td>Price</td>
</tr>
<th><h2 style="text-align:left;">Cameras</h2></th>
<tr>
<?php $i=1;
$qry_cam=mysql_query("SELECT* FROM camera ORDER BY camera.price ASC",$con);
while($row=mysql_fetch_array($qry_cam))
{
echo "<tr id='cam_as'>
<td>$i-$row[title]<br /></td>
<td>$row[price]<br /></td>
</tr>";
$i++;
}
?>
</tr>
<tr>
<?php $i=1;
$qry_cam=mysql_query("SELECT* FROM camera ORDER BY camera.price DESC",$con);
while($row=mysql_fetch_array($qry_cam))
{
echo "<tr id='cam_ds' style='display:none;'>
<td>$i-$row[title]<br /></td>
<td>$row[price]<br /></td>
</tr>";
$i++;
}
?>
</tr>
</table>
</div><!-- style ends-->
Finally, here is the script which is doing this all required work:
<script type="text/javascript">
function as()
{
$('#cam_as,#com_as,#cell_as').css('display','block');
$('#cam_ds,#com_ds,#cell_ds').css('display','none');
}
function ds()
{
$('#cam_ds,#com_ds,#cell_ds').css('display','block');
$('#cam_as,#com_as,#cell_as').css('display','none');
}
</script>
I have tried but I am unable to find where I am going wrong.
Yoo have enough problems with your code. I removed extra < tr >'s. I added classes and changed javascript functions accordingly. Your problem is mainly in nested < tr >'s
http://jsfiddle.net/Sanya_Zol/kC9Tk/
<button onclick="as()">Ascending Order</button>
<button onclick="ds()">Descding Order</button>
<div class="style">
<table>
<tr>
<td>Title</td>
<td>Price</td>
</tr>
<th><h2 style="text-align:left;">Cameras</h2></th>
<?php $i=1;
$qry_cam=mysql_query("SELECT* FROM camera ORDER BY camera.price ASC",$con);
while($row=mysql_fetch_array($qry_cam))
{
echo "<tr class='cam_as'>
<td>$i-$row[title]<br /></td>
<td>$row[price]<br /></td>
</tr>";
$i++;
}
?>
<?php $i=1;
$qry_cam=mysql_query("SELECT* FROM camera ORDER BY camera.price DESC",$con);
while($row=mysql_fetch_array($qry_cam))
{
echo "<tr class='cam_ds' style='display:none;'>
<td>$i-$row[title]<br /></td>
<td>$row[price]<br /></td>
</tr>";
$i++;
}
?>
</table>
</div>
<script type="text/javascript">
function as(){
$('.cam_as,#com_as,#cell_as').show();
$('.cam_ds,#com_ds,#cell_ds').hide();
}
function ds(){
$('.cam_ds,#com_ds,#cell_ds').show();
$('.cam_as,#com_as,#cell_as').hide();
}
$(as); // run as() when document ready
</script>
There are <tr>Tag outside and inside the loop, you only need them one time.
The loop would also create multiple <tr> Tags with the same ID, thats not correct. Try to use jQuery-Toggle to show/hide the elements.
The last part of the question is not clear for me, sorry.

Categories