...

View Full Version : Php output as table



bucket
08-25-2009, 08:40 PM
Hello I need to combine this table with my php form:

Basicly when someone registers the html table will be updated with another row

Here is the PHP:




<?php

require("inc/config.php");
if (isset($_POST['del']))
{

for ($count = 0;$count<count($_POST[delchk]);$count++)
{
$delete = $_POST[delchk][$count];
$query = "DELETE FROM persons WHERE id = '$delete'";
$result = mysql_query($query);
if (!$result)
{
die("Error deleting persons! Query: $query<br />Error: ".mysql_error());
}
}
}
$result = mysql_query("SELECT * FROM persons");

// Check how many rows it found
if(mysql_num_rows($result) > 0){
echo "<input type='button' onclick='checkall(document.myform.delchk);' value='Select All'>";
echo "<input type='button' onclick='uncheckall(document.myform.delchk);' value='UnSelect All'>";
echo "<hr>";
echo "<form name = 'myform' action='' method='post'>";
while($row = mysql_fetch_array($result))
{
echo "First Name: " . $row['FirstName'] . "";
echo "<br>";
echo "Last Name: " . $row['LastName'] . "";
echo "<br>";
echo "<a target=frame2 href='" ."profile.php?user1=". $row['FirstName'] ."'>Profile</a>";
echo "<br>";
echo "Date: " . $row['AddedDate'] . "";
echo "<br>";
echo "IP Address: " . $row['Ip'] . "";
echo "<br>";
echo 'Select to delete: <input type="checkbox" id="delchk" name="delchk[]" value="'.$row['id'].'" />';
echo "<br>";
echo "***********************************************";
echo "<br>";
}
echo "<hr>";
echo "<input type='submit' name = 'del' value='Delete Selected'></form>";
}

else{
// No rows were found ...
echo 'No one has yet to register.';
}

mysql_close($con);
?>

Here is the html table:

<table id="mytable">
<thead>

<tr>

<th scope="col">Delete?</th>
<th scope="col">First Name</th>
<th scope="col">Last Name</th>
<th scope="col">Profile</th>
<th scope="col">Date</th>
<th scope="col">IP Address</th>

</tr>
</thead>
<tbody>

<tr>
<td>CheckBox</td>
<td>John</td>
<td>Mclan</td>
<td>Profile</td>
<td>August 25 2009</td>
<td>90.209.15.233</td>

</tr>
</tbody>

</table>



Basicly when someone registers it adds this as another row.

<tr>
<td>CheckBox</td>
<td>John</td>
<td>Mclan</td>
<td>Profile</td>
<td>August 25 2009</td>
<td>90.209.15.233</td>

</tr>

Can someone help me out??

whizard
08-25-2009, 08:43 PM
You mean that you would like a new header row to be added with each info row?

Dan

bucket
08-25-2009, 08:48 PM
No just a row with the clients information.

Like a new row with 6 cells with each one containing a separate piece of the users information.

I tried it myself but it didn't work.




<?php

require("inc/config.php");
if (isset($_POST['del']))
{

for ($count = 0;$count<count($_POST[delchk]);$count++)
{
$delete = $_POST[delchk][$count];
$query = "DELETE FROM persons WHERE id = '$delete'";
$result = mysql_query($query);
if (!$result)
{
die("Error deleting persons! Query: $query<br />Error: ".mysql_error());
}
}
}
$result = mysql_query("SELECT * FROM persons");

// Check how many rows it found
if(mysql_num_rows($result) > 0){
echo "<table id="mytable">
<thead>

<tr>
<th scope="col">Delete?</th>
<th scope="col">First Name</th>
<th scope="col">Last Name</th>
<th scope="col">Profile</th>
<th scope="col">Date</th>
<th scope="col">IP Address</th>
</tr>
</thead>
<tbody>
</tr>";

echo "<input type='button' onclick='checkall(document.myform.delchk);' value='Select All'>";
echo "<input type='button' onclick='uncheckall(document.myform.delchk);' value='UnSelect All'>";
echo "<hr>";
echo "<form name = 'myform' action='' method='post'>";
while($row = mysql_fetch_array($result))
{
echo "<td>First Name: " . $row['FirstName'] . "</td>";
echo "<br>";
echo "<td>Last Name: " . $row['LastName'] . "</td>";
echo "<br>";
echo "<td><a target=frame2 href='" ."profile.php?user1=". $row['FirstName'] ."'>Check HighScores</a></td>";
echo "<br>";
echo "<td>Date: " . $row['AddedDate'] . "</td>";
echo "<br>";
echo "<td>IP Address: " . $row['Ip'] . "</td>";
echo "<br>";
echo 'Select to delete: <input type="checkbox" id="delchk" name="delchk[]" value="'.$row['id'].'" />';
echo "<br>";
echo "***********************************************";
echo "<br>";
}
echo "<hr>";
echo "</tr>";
echo "</tbody>";
echo "</tbody>";
echo "<input type='submit' name = 'del' value='Delete Selected'></form>";

}

else{
// No rows were found ...
echo 'No one has yet registered.';
}

mysql_close($con);
?>

Example--
Before someone signs up:
http://i27.tinypic.com/2604h7m.jpg

After another person signs up:
http://i31.tinypic.com/xold87.jpg
(Basically adding another row)

whizard
08-25-2009, 08:55 PM
Basicly when someone registers it adds this as another row.

So when you wrote this did you mean that's what happens NOW or that's what you'd like to happen?

Dan

bucket
08-25-2009, 09:02 PM
Yes, I would like another row to show up. I made a picture In my last post.

Example--
Before someone signs up:
http://i27.tinypic.com/2604h7m.jpg

After another person signs up:
http://i31.tinypic.com/xold87.jpg
(Basically adding another row)

It shows what I would like to happen but cant figure out why the page shows up blank...

whizard
08-25-2009, 09:04 PM
<table id="mytable">
<thead>

<tr>
<th scope="col">Delete?</th>
<th scope="col">First Name</th>
<th scope="col">Last Name</th>
<th scope="col">Profile</th>
<th scope="col">Date</th>
<th scope="col">IP Address</th>
</tr>
</thead>
<tbody>
</tr>";


You need to use \ to escape every " in that part (except for the very first and very last). Each time the script encounters a " it switches out of string mode and into php mode, which you don't want. Do you have error reporting turned off?

Dan

bucket
08-25-2009, 09:06 PM
I dont know if I have error turned off. Can you post a working version of it?

And also where would I add \ too?

whizard
08-25-2009, 09:09 PM
1. Add this line: error_reporting(E_ALL); to the top of the code (inside the <?php of course)

2. the backslash is an 'escape' character. What this means is that you place it in front of a character which has a special meaning, but you want the code to ignore, such as " or $.

So, to escape the code, place a backslash ( \ ) in front of every " you have in that block of code.

Dan

bucket
08-25-2009, 09:14 PM
Its not showing errors...
Even tho I added it.

Can you show me where to put the \ in the code?



<?php
error_reporting(E_ALL);
require("inc/config.php");
if (isset($_POST['del']))
{

for ($count = 0;$count<count($_POST[delchk]);$count++)
{
$delete = $_POST[delchk][$count];
$query = "DELETE FROM persons WHERE id = '$delete'";
$result = mysql_query($query);
if (!$result)
{
die("Error deleting persons! Query: $query<br />Error: ".mysql_error());
}
}
}
$result = mysql_query("SELECT * FROM persons");

// Check how many rows it found
if(mysql_num_rows($result) > 0){
echo "<table id="mytable">
<thead>

<tr>
<th scope="col">Delete?</th>
<th scope="col">First Name</th>
<th scope="col">Last Name</th>
<th scope="col">Profile</th>
<th scope="col">Date</th>
<th scope="col">IP Address</th>
</tr>
</thead>
<tbody>
</tr>";

echo "<input type='button' onclick='checkall(document.myform.delchk);' value='Select All'>";
echo "<input type='button' onclick='uncheckall(document.myform.delchk);' value='UnSelect All'>";
echo "<hr>";
echo "<form name = 'myform' action='' method='post'>";
while($row = mysql_fetch_array($result))
{
echo "<td>First Name: " . $row['FirstName'] . "</td>";
echo "<br>";
echo "<td>Last Name: " . $row['LastName'] . "</td>";
echo "<br>";
echo "<td><a target=frame2 href='" ."profile.php?user1=". $row['FirstName'] ."'>Check HighScores</a></td>";
echo "<br>";
echo "<td>Date: " . $row['AddedDate'] . "</td>";
echo "<br>";
echo "<td>IP Address: " . $row['Ip'] . "</td>";
echo "<br>";
echo 'Select to delete: <input type="checkbox" id="delchk" name="delchk[]" value="'.$row['id'].'" />';
echo "<br>";
echo "***********************************************";
echo "<br>";
}
echo "<hr>";
echo "</tr>";
echo "</tbody>";
echo "</tbody>";
echo "<input type='submit' name = 'del' value='Delete Selected'></form>";

}

else{
// No rows were found ...
echo 'No one has yet registered.';
}

mysql_close($con);
?>

whizard
08-25-2009, 09:16 PM
OK Change



echo "<table id="mytable">
<thead>

<tr>
<th scope="col">Delete?</th>
<th scope="col">First Name</th>
<th scope="col">Last Name</th>
<th scope="col">Profile</th>
<th scope="col">Date</th>
<th scope="col">IP Address</th>
</tr>
</thead>
<tbody>
</tr>";


to



echo "<table id=\"mytable\">
<thead>

<tr>
<th scope=\"col\">Delete?</th>
<th scope=\"col\">First Name</th>
<th scope=\"col\">Last Name</th>
<th scope=\"col\">Profile</th>
<th scope=\"col\">Date</th>
<th scope=\"col\">IP Address</th>
</tr>
</thead>
<tbody>
</tr>";


Then tell me what happens

Dan

bucket
08-25-2009, 09:25 PM
I think I have fully fixed it. It is now showing up:



<?php
error_reporting(E_ALL);

require("inc/config.php");
if (isset($_POST['del']))
{

for ($count = 0;$count<count($_POST[delchk]);$count++)
{
$delete = $_POST[delchk][$count];
$query = "DELETE FROM persons WHERE id = '$delete'";
$result = mysql_query($query);
if (!$result)
{
die("Error deleting persons! Query: $query<br />Error: ".mysql_error());
}
}
}
$result = mysql_query("SELECT * FROM persons");

// Check how many rows it found
if(mysql_num_rows($result) > 0){
echo "<table id=\"mytable\">
<thead>

<tr>
<th scope=\"col\">Delete?</th>
<th scope=\"col\">First Name</th>
<th scope=\"col\">Last Name</th>
<th scope=\"col\">Profile</th>
<th scope=\"col\">Date</th>
<th scope=\"col\">IP Address</th>
</tr>
</thead>
<tbody>";


echo "<form name = 'myform' action='' method='post'>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['FirstName'] . "</td>";
echo "<td>" . $row['LastName'] . "</td>";
echo "<td><a target=frame2 href='" ."http://hiscore.runescape.com/hiscorepersonal.ws?user1=". $row['FirstName'] ."'>Check HighScores</a></td>";
echo "<td>" . $row['AddedDate'] . "</td>";
echo "<td>" . $row['Ip'] . "</td>";
echo '<td><input type="checkbox" id="delchk" name="delchk[]" value="'.$row['id'].'" /></td>';
echo "</tr>";
}
echo "</tbody>";
echo "</table>";
echo "<input type='submit' name = 'del' value='Delete Selected'></form>";
echo "<input type='button' onclick='checkall(document.myform.delchk);' value='Select All'>";
echo "<input type='button' onclick='uncheckall(document.myform.delchk);' value='UnSelect All'>";

}

else{
// No rows were found ...
echo 'Your account list is clear. Please wait for people to login.';
}

mysql_close($con);
?>

But the only thing is, how do I make certain cells smaller than others, like the width.

whizard
08-25-2009, 09:29 PM
You can do that either by using the width attribute in each <td>

<td width="150">

Or through CSS:

.narrow
{
width:150px;
}

<td class="narrow">

Dan

bucket
08-25-2009, 09:36 PM
Thanks.

This is the finished code:

Please comment on what you think should be changed, is unneeded, or what should should be added.


<?php
error_reporting(E_ALL);

require("inc/config.php");
if (isset($_POST['del']))
{

for ($count = 0;$count<count($_POST[delchk]);$count++)
{
$delete = $_POST[delchk][$count];
$query = "DELETE FROM persons WHERE id = '$delete'";
$result = mysql_query($query);
if (!$result)
{
die("Error deleting persons! Query: $query<br />Error: ".mysql_error());
}
}
}
$result = mysql_query("SELECT * FROM persons");

// Check how many rows it found
if(mysql_num_rows($result) > 0){
echo "<table id=\"mytable\">
<thead>

<tr>
<th align=\"center\" scope=\"col\">Delete?</th>
<th align=\"center\" scope=\"col\">First Name</th>
<th align=\"center\" scope=\"col\">Last Name</th>
<th align=\"center\" scope=\"col\">Profile</th>
<th align=\"center\" scope=\"col\">Date</th>
<th align=\"center\" scope=\"col\">IP Address</th>
</tr>
</thead>
<tbody>";


echo "<form name = 'myform' action='' method='post'>";
while($row = mysql_fetch_array($result))
{
echo "<tr align=\"center\">";
echo '<td><input type="checkbox" id="delchk" name="delchk[]" value="'.$row['id'].'" /></td>';
echo "<td>" . $row['FirstName'] . "</td>";
echo "<td>" . $row['LastName'] . "</td>";
echo "<td><a target=frame2 href='" ."profile.php?user1=". $row['FirstName'] ."'>Check Profile</a></td>";
echo "<td>" . $row['AddedDate'] . "</td>";
echo "<td>" . $row['Ip'] . "</td>";
echo "</tr>";
}
echo "</tbody>";
echo "</table>";
echo "<hr>";
echo "<input type='submit' name = 'del' value='Delete Selected'></form>";
echo "<input type='button' onclick='checkall(document.myform.delchk);' value='Select All'>";
echo "<input type='button' onclick='uncheckall(document.myform.delchk);' value='UnSelect All'>";

}

else{
// No rows were found ...
echo 'No registered members.';
}

mysql_close($con);
?>

whizard
08-25-2009, 09:51 PM
It looks good, I would just say make sure you are checking the POST values, so that someone couldn't mess with your script.

Dan

bucket
08-25-2009, 09:56 PM
You mean so someone cant click register2.php like 6 times and create blank or multiple accounts?

whizard
08-25-2009, 10:01 PM
Well, make sure

a) the POST values have been set (are not empty)
b) the POST values have the right kind of data in them. Are you expecting an int? use is_int(). Are you expecting a string? Use is_string().
c) the POST values have been escaped to avoid SQL injection. See mysql_real_escape_string() (http://us.php.net/manual/en/function.mysql-real-escape-string.php)

Dan

bucket
08-25-2009, 11:33 PM
Hm... now for some reason the select all button isnt working...

Current code:



<?php
error_reporting(E_ALL);

require("inc/config.php");
if (isset($_POST['del']))
{

for ($count = 0;$count<count($_POST[delchk]);$count++)
{
$delete = $_POST[delchk][$count];
$query = "DELETE FROM persons WHERE id = '$delete'";
$result = mysql_query($query);
if (!$result)
{
die("Error deleting persons! Query: $query<br />Error: ".mysql_error());
}
}
}
$result = mysql_query("SELECT * FROM persons");

// Check how many rows it found
if(mysql_num_rows($result) > 0){
echo "<table id=\"mytable\">
<thead>

<tr>
<th align=\"center\" scope=\"col\">Delete</th>
<th align=\"center\" scope=\"col\">First Name</th>
<th align=\"center\" scope=\"col\">Last Name</th>
<th align=\"center\" scope=\"col\">High Scores</th>
<th align=\"center\" scope=\"col\">Date of Entry</th>
<th align=\"center\" scope=\"col\">IP Address</th>
</tr>
</thead>
<tbody>";


echo "<form name = 'myform' action='' method='post'>";
while($row = mysql_fetch_array($result))
{
echo "<tr align=\"center\">";
echo '<td><input type="checkbox" id="delchk" name="delchk[]" value="'.$row['id'].'" /></td>';
echo "<td>" . $row['FirstName'] . "</td>";
echo "<td>" . $row['LastName'] . "</td>";
echo "<td><a target=frame2 href='" ."profile.php?user1=". $row['FirstName'] ."'>Check HighScores</a></td>";
echo "<td>" . $row['AddedDate'] . "</td>";
echo "<td>" . $row['Ip'] . "</td>";
echo "</tr>";
}
echo "</tbody>";
echo "</table>";
echo "<hr>";
echo "<input type='submit' name = 'del' value='Delete Selected'></form>";
echo "<input type='button' onclick='checkall(document.myform.delchk);' value='Select All'>";
echo "<input type='button' onclick='uncheckall(document.myform.delchk);' value='UnSelect All'>";

}

else{
// No rows were found ...

echo "No Registered Members";


echo "</tbody>";
echo "</table>";
}

mysql_close($con);
?>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum