PHP Re-submit form - php

I have a script that is activated when someone submits a form on that same page. The first time the user fills in data and presses submit, it works, the second time it does not respond.
NOTE: There is a second document where the sessions are created (SESSION_START is given).
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
$_SESSION['aantalpogingen'] = $_SESSION['aantalpogingen'] + 1;
echo $_SESSION['aantalpogingen'];
echo "<br />";
$_SESSION['poging'][$_SESSION['aantalpogingen']] = substr($_SESSION['hetwoord'] , 0 , 1) . $_POST['PogingLetter2'] . $_POST['PogingLetter3'] . $_POST['PogingLetter4'] . $_POST['PogingLetter5'];
echo "<table width='450' height='75' border='1'>";
foreach($_SESSION['poging'] as $pogingnr=>$gok)
{
if($gok != "")
{
echo "<tr>";
echo "<th WIDTH='66'>";
echo $gok[0] ;
echo "</th>";
echo "<th WIDTH='66'>";
echo $gok[1] ;
echo "</th>";
echo "<th WIDTH='66'>";
echo $gok[2] ;
echo "</th>";
echo "<th WIDTH='66'>";
echo $gok[3] ;
echo "</th>";
echo "<th WIDTH='66'>";
echo $gok[4] ;
echo "</th>";
echo "<th>";
echo "Poging " . $pogingnr;
echo "</th>";
echo "</tr>";
}
}
echo " </table";
}
?>
I cannot find any syntax errors or anything, I hope you can help me :)

I found at least one mistake :->
echo " </table";
missing >
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
This condition meant to be for the not intended below too?
echo "<table width='450' height='75' border='1'>";
As it's not closed before the echo.

Related

Drupal SQL table how to insert edit button on each row

I've got a table in Drupal with the following code:
$db = mysql_connect("localhost", "root", "moocow");
mysql_select_db("vedb", $db);
$result = mysql_query("SELECT NodeID,NodeDesc,NodeZone,Fusion,DSLID FROM `nodeidtable` WHERE DSLID != '' AND `NodeZone` = 'CLOSED' ORDER BY NodeID ASC");
$num_rows = mysql_num_rows($result);
echo "<table>";
echo "<tr>";
echo "<th>CLOSED SITES</th>";
echo "<th></th>";
echo "<th></th>";
echo "<th></th>";
echo "<th></th>";
echo "</tr>";
echo "<tr>";
echo "<th>Node ID</th>";
echo "<th>Node Address</th>";
echo "<th>Node Zone</th>";
echo "<th>Fusion Status</th>";
echo "<th>Service Number</th>";
echo "</tr>";
//display the data
while ($rows = mysql_fetch_array($result,MYSQL_ASSOC))
{
echo "<tr>";
foreach ($rows as $data)
{
echo "<td align='center'>". $data . "</td>";
}
}
echo "<br>";
echo "<tr>";
echo "</table>";
mysql_free_result($result);
mysql_close($db);
?>
Now I can change it renders the td to include the individual columns, but I really want to add a little edit button on the right-hand side which will let me edit that particular row fields.
Any ideas?
Replace your while loop with the following code:
while ($rows = mysql_fetch_array($result,MYSQL_ASSOC))
{
echo "<tr>";
foreach ($rows as $data)
{
echo "<td align='center'>". $data . "</td>";
}
//create link for current node edit
echo "<td align='center'>". l(t('Edit this node'), 'node/' . $row['NodeId'] . '/edit') ."</td>";
echo "</tr>";
}
Remove echo "<br>"; and echo "<tr>"; below to while loop. This will resolve the issue for you

Why does PHP nested foreach loop displays only first row from sql database

I am trying to display a table from database using two nested foreach loops with some criteria. here is the code-
echo "<div class='container'>";
echo "<table class='table table-hover table-bordered table-condensed' style='width:95%' align='center'>";
echo "<tr>";
echo "<th>";
echo "Edit";
echo "</th>";
echo "<th>";
echo "Delete";
echo "</th>";
echo "<th>";
echo "Sl. No.";
echo "</th>";
echo "<th>";
echo "Group";
echo "</th>";
echo "<th>";
echo "Component";
echo "</th>";
echo "<th>";
echo "Quantity";
echo "</th>";
echo "</tr>";
if($rslt->rowCount() > 0)
{
foreach($rslt as $item)
{
foreach($rslt3 as $item3)
{
/*echo $item3['component'];
if($item3['component']===$item['component'])
{
if($Qty>=$item3['Qty'])
{
$item3[Qty]=$item3[Qty]-$item[Qty];
*/
//will implement this after the second loop starts working
$id = $item['entry_id'];
$Qty = $item['Qty'];
$group_ID = $item['group_ID'];
$component = $item['component'];
$vendor_ID = $item['vendor_ID'];
echo "<tr>";
echo "<td>";
echo "<a href='production_edit.php?id=$id&Qty=$Qty&group_ID=$group_ID&component=$component&vendor_ID=$vendor_ID'>Edit</a>";
echo "</td>";
echo "<td>";
echo "<a href='production_delete.php?id=$id&vendor_ID=$vendor_ID'>Delete</a>";
echo "</td>";
echo "<td>";
echo $item['entry_id'];
echo "</td>";
echo "<td>";
echo $item['group_ID'];
echo "</td>";
echo "<td>";
echo $item['component'];
echo "</td>";
echo "<td>";
echo $item['Qty'];
echo "</td>";
echo "</tr>";
}
}
}
echo "</table></div><br>";
}
Now the problem here is when I use the second foreach loop the table displays the first entry in each table row... I am curious where I am at fault with this second foreach loop.. Thanks in advance

How to generate HTML table with nested loop?

I have a table that should look like the following:
http://hell-rider.de/Fotos/img/beispiel-1.JPG
I have the following PHP script:
foreach ($xml->result->rowset->row as $row){
if($row{'refTypeID'} == 42){
echo '<tr>';
echo "<th>";
echo $row{'date'};
echo "</th>";
echo "<th>";
echo '' . $row{'ownerName1'} .'';
echo "</th>";
echo "<th>";
foreach ($allgemeinxml->result->rowset->row as $type){
echo '' . $type{'typeName'} .'';
}
echo "</th>";
echo "<th>";
echo '<p class="minus">' . $row{'amount'} .' ISK</p>';
echo "</th>";
echo "<th>";
echo '<p class="kontostand">' . $row{'balance'} . ' ISK</p>';
echo "</th>";
echo '</tr>';
}
}
echo "</table>";
The actual output of my script is the following, however:
http://hell-rider.de/Fotos/img/beispiel2.JPG
How do I have to change my script to populate the third column correctly (and not with MetallurgyTradeLaboratory OperationLaboratory OperationLaboratory OperationResearchCybernetics)?
<th> tags are for header cells, you should be using <td> data cells.
$types = array();
foreach ($allgemeinxml->result->rowset->row as $type){
$types[] = $type{'typeName'};
}
$type_index = 0;
foreach ($xml->result->rowset->row as $row){
if($row{'refTypeID'} == 42){
echo '<tr>';
echo "<td>";
echo $row{'date'};
echo "</td>";
echo "<td>";
echo '' . $row{'ownerName1'} .'';
echo "</td>";
echo "<td>";
echo $types[$type_index];
echo "</td>";
echo "<td>";
echo '<p class="minus">' . $row{'amount'} .' ISK</p>';
echo "</td>";
echo "<td>";
echo '<p class="kontostand">' . $row{'balance'} . ' ISK</p>';
echo "</td>";
echo '</tr>';
}
$type_index++;
}
echo "</table>";
I don't know exactly what the use of if($row{'refTypeID'} == 42){ is, so you either want $type_index++; in the place I've put it above, or just above the closing brace }. For this to work well, $allgemeinxml->result->rowset->row and $xml->result->rowset->row need the same number of elements.

Set background color of cells based on MySQL query output

I am new to PHP and trying to learn enough to do some basic functions. I've been able to create a table for my users to edit themselves, and redisplay but I've come across a question.
Using the script below, users can input their skill level for various products. I wanted to be able to highlight each cell in which they input "0" or blank. User's input will be between 0-5 (or blank if they haven't filled it in yet).
This is all being done on my localhost so I'll admit all the security measures are not quite there.
I've read a lot of posts and tried to figure it out myself, but I'm doing something fundamentally wrong I believe.
Any assistance on this would be greatly appreciated. I've been known to buy a beer (via paypal) for those who help me with coding :)
Here is my existing code for printing out the results of the database:
<?php
//This will connect to the database in order to begin this page
mysql_connect("localhost", "root", "time2start") or die (mysql_error());
//Now we will select the database we need to talk to
mysql_select_db("joomla_dev_15") or die (mysql_error());
$query = "SELECT * FROM enterprise_storage WHERE id=1";
$result = mysql_query($query) or die (mysql_error());
echo "<table border='1'>";
echo "$row";
echo "<tr> <th>Product</th> <th>Wayne Beeg</th> <th>Paul Hamke</th> <th>Steve Jaczyk</th> <th>David Jontow</th> <th>Ed MacDonald</th> <th>Michael Munozcano</th> <th>Ron Shaffer</th> <th>Luke Soares</th> <th>Josh Wenger</th> </tr>";
// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result )) {
// Print out the contents of each row into a table
echo "<tr><td>";
echo $row['model'];
echo "</td><td>";
echo $row['beeg'];
echo "</td><td>";
echo $row['hamke'];
echo "</td><td>";
echo $row['jaczyk'];
echo "</td><td>";
echo $row['jontow'];
echo "</td><td>";
echo $row['macdonald'];
echo "</td><td>";
echo $row['munozcano'];
echo "</td><td>";
echo $row['shaffer'];
echo "</td><td>";
echo $row['soares'];
echo "</td><td>";
echo $row['wenger'];
echo "</td></tr>";
}
echo "</table>";
?>
<FORM>
<INPUT TYPE="BUTTON" VALUE="Return to the Home Page" ONCLICK="window.location.href='http://localhost/~user/joomla15/custom/skilldisplay.php'">
</FORM>
Maybe
while($row = mysql_fetch_array( $result )) {
// Print out the contents of each row into a table
echo "<tr>";
foreach($row as $content) {
if($content == 0) {
echo "<td style='background-color:gray;'>";
}
else {
echo "<td style='background-color:green;'>";
}
echo $content . "</td>";
}
echo $row['wenger'];
echo "</td>";
}
echo "</tr></table>";
try something like this
add this to of your generated document
<style type="text/css">
.red{ background-color: red; }
</style>
This is your PHP:
<?php
// sanitize value
$value = trim($row['model']);
$class = (empty($value)) ? 'red' : '';
// display
echo "<td class=\"$class\">$value</td>";
...
?>
Ok, so I managed to get it working finally. The two replies above helped me figure out the right approach to doing this.
Of course, my approach may not be the best method, but I've tested it and it works for my needs. For any future searchers, here's what I did:
<?php
//This will connect to the database in order to begin this page
mysql_connect("localhost", "root", "time2start") or die (mysql_error());
//Now we will select the database we need to talk to
mysql_select_db("joomla_dev_15") or die (mysql_error());
$query = "SELECT * FROM enterprise_storage";
$result = mysql_query($query) or die (mysql_error());
echo "<table border='1'>";
echo "$row";
echo "<tr> <th>Product</th> <th>Wayne Beeg</th> <th>Paul Hamke</th> <th>Steve Jaczyk</th> <th>David Jontow</th> <th>Ed MacDonald</th> <th>Michael Munozcano</th> <th>Ron Shaffer</th> <th><a href='http://localhost/~user/joomla15/custom/updateform.php'>Luke Soares</a></th> <th>Josh Wenger</th> </tr>";
// keeps getting the next row until there are no more to get
while($row = mysql_fetch_array( $result )) {
// Print out the contents of each row into a table
echo "<tr><td>";
echo $row['model'];
echo "</td>";
if ($row['beeg'] == '0'){
echo '<td bgcolor="#FF0000">' . $row['beeg'] ;
}else{
echo '<td>' .$row['beeg'];
}
echo "</td>";
if ($row['hamke'] == '0'){
echo '<td bgcolor="#FF0000">' . $row['hamke'] ;
}else{
echo '<td>' .$row['hamke'];
}
echo "</td>";
if ($row['jaczyk'] == '0'){
echo '<td bgcolor="#FF0000">' . $row['jaczyk'] ;
}else{
echo '<td>' .$row['jaczyk'];
}
echo "</td>";
if ($row['jontow'] == '0'){
echo '<td bgcolor="#FF0000">' . $row['jontow'] ;
}else{
echo '<td>' .$row['jontow'];
}
echo "</td>";
if ($row['macdonald'] == '0'){
echo '<td bgcolor="#FF0000">' . $row['macdonald'] ;
}else{
echo '<td>' .$row['macdonald'];
}
echo "</td>";
if ($row['munozcano'] == '0'){
echo '<td bgcolor="#FF0000">' . $row['munozcano'] ;
}else{
echo '<td>' .$row['munozcano'];
}
echo "</td>";
if ($row['shaffer'] == '0'){
echo '<td bgcolor="#FF0000">' . $row['shaffer'] ;
}else{
echo '<td>' .$row['shaffer'];
}
echo "</td>";
if ($row['soares'] == '0'){
echo '<td bgcolor="#FF0000">' . $row['soares'] ;
}else{
echo '<td>' .$row['soares'];
}
echo "</td>";
if ($row['wenger'] == '0'){
echo '<td bgcolor="#FF0000">' . $row['wenger'] ;
}else{
echo '<td>' .$row['wenger'];
}
echo "</td></tr>";
}
echo "";
?>

Set or Assign ID or REL inside PHP code

I want to set or assign id / rel inside my table code, take a look on my code below this so every table td has id and rel so I can give statement and manipulate the date within the jQuery, any helps will be much more appreciated:
function printdata()
{
if(is_array($_SESSION['chart']))
{
echo "Here's your chart" . "<br>";
$max = count($_SESSION['chart']);
$th1 = "<th>" . "No" . "</th>";
$th2 = "<th>" . "Nama Barang" . "</th>";
$th3 = "<th>" . "Harga Barang" . "</th>";
$th4 = "<th>" . "Stok Barang" . "</th>";
echo "<table border=1>";
echo "<tr>";
echo $th1 ;
echo $th2;
echo $th3;
echo $th4;
echo "</tr>";
for ($indexo = 0; $indexo < $max; $indexo++) {
echo "<tr>";
echo "<td>" . $indexo."</td>";
echo "<td >" . $_SESSION['chart'][$indexo]['namabarang']."</td>";
//i want to assign td rel inside of this echo
echo "<td>" . $_SESSION['chart'][$indexo]['hargabarang']."</td>";
echo "<td>" . $_SESSION['chart'][$indexo]['stokbarang']."</td>";
echo " <td><button id=".$indexo." name=\"button\">Edit</button></td>";
echo "</tr>";
}
echo "</table>";
}else
{
echo "Chart is still Empty";
}
}
echo "<table border=1 id=\"{$some_php_variable_with_an_id_value_in_it}\">"
first your code is too much line over there, that is no need to do such as that, it can be more simple to build some output with the same result, here may be could to help ya
function printdata(){
if(is_array($_SESSION['cart'])){
echo "Here's Your Chart ".count($_SESSION['cart'])."<br />";
echo '<table><tr>th>No</th><th>Nama Barang</th><th>Harga Barang</th><th>Stok Barang</th><th>Action</th></tr>';
for($indexo =0; $indexp < count($_SESSION['cart']); $indexo++){
echo "<tr>
<td id=".$indexo.">".$indexo."</td>
<td id=".$_SESSION['chart'][$indexo]['namabarang'].">".$_SESSION['chart'][$indexo]['namabarang']."<td>
<td id=".$_SESSION['chart'][$indexo]['hargabarang'].">".$_SESSION['chart'][$indexo]['hargabarang']."<td>
<td id="$_SESSION['chart'][$indexo]['stokbarang']">".$_SESSION['chart'][$indexo]['stokbarang']."<td>
<td><button id=".$indexo." name=\"button\">Edit</button></td>
</tr>";
}
}else{
echo "Chart is still Empty";
}
}
You can Change <td id=".$indexo."> to what ever you want to, seems like the other, id or rel will be the same implementation i guest

Categories