Solved

Help with taking data from a DB table into an HTML table

Posted on 2008-10-04
10
245 Views
Last Modified: 2008-10-04
Hiya -

I currently have code that takes data from a DB table and puts it into an HTML table.

Now, I want to put a DELETE button in the last cell of each row... that when press, will delete that entry from the DB. I know how to do this normally but not using this "IMPLODE" function that someone here advised me to use. I just need to know how to replace the 2 "????????"'s in the attached code.
$query="SELECT * FROM categories";
	$result = mysql_query($query) or die("Unable to execute $query: " . mysql_error());
 
 
        $row = mysql_fetch_assoc($result);
	$colorvar = 1;
 
        do{
		if ($colorvar == 1)
		{
                	echo '<tr><td bgcolor="#FFFFDF"><font color="#660033" face="calibri">'. implode('</td><td bgcolor="#FFFFDF"><font color="#660033" face="calibri">',$row) .'</td><td bgcolor="#FFFFDF"><form action="./budget.php"><input type="hidden" name="delete" value="1"><input type="hidden" name="id" value="???????"><center><input type="submit" value="Delete"></center></form></td></tr>';
			$colorvar = 2;
		} else
		{
	        	echo '<tr><td><font color="#660033" face="calibri">'. implode('</td><td><font color="#660033" face="calibri">',$row) .'</td><td><form action="./budget.php"><input type="hidden" name="delete" value="1"><input type="hidden" name="id" value="???????????"><center><input type="submit" value="Delete"></center></form></td></tr>';
			$colorvar = 1;
		}
 
		$resultcount = $resultcount + 1;
        }while( $row = mysql_fetch_assoc($result) );
 
print '		      <tr><form action="./budget.php">
        	<td width="5%" bgcolor="#DDFFFF"><font color="#FFFFCC" face="calibri"><input type="hidden" name="create" value="1"></td>
        	<td width="5%" bgcolor="#DDFFFF"><font color="#FFFFCC" face="calibri"></td>
        	<td width="10%" bgcolor="#DDFFFF"><font color="#FFFFCC" face="calibri"><input type="text" name="type"></td>
        	<td width="25%" bgcolor="#DDFFFF"><font color="#FFFFCC" face="calibri"><input type="text" name="main"></td>
        	<td width="25%" bgcolor="#DDFFFF"><font color="#FFFFCC" face="calibri"><input type="text" name="secondary"></td>
        	<td width="10%" bgcolor="#DDFFFF"><font color="#FFFFCC" face="calibri"></td>
        	<td width="10%" bgcolor="#DDFFFF"><font color="#FFFFCC" face="calibri"></td>
        	<td width="10%" bgcolor="#DDFFFF"><font color="#FFFFCC" face="calibri"><center><input type="submit" value="Create"></center></td>
		      </form></tr></tbody></table></body></html>';
 
mysql_free_result($result);

Open in new window

0
Comment
Question by:runelynx
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
10 Comments
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 22642816
If the code you have creates the appropriate HTML and you can see the hidden ID value in the source code of the HTML page generated (when you click view source from browser), then all you need to do is code the DELETE button to post data to a PHP page that then uses the posted ID like:

<?php
$id = $_POST["id"];
?>

Then in your SQL statement do this:

$query = "DELETE FROM categories WHERE id = $id";

Unless I missed something, this should work.
0
 

Author Comment

by:runelynx
ID: 22642821
Hi,

Thanks but the problem I'm having is I can't pull out the "id" value to assign into a variable. I'm not sure how to attach it to a variable while the "implode" function is working in the loop.
0
 

Author Comment

by:runelynx
ID: 22642822
Re working the code snippet to make a little more sense

 $row = mysql_fetch_assoc($result);
        $colorvar = 1;
 
        do{
                if ($colorvar == 1)
                {
                        echo '<tr><td bgcolor="#FFFFDF"><font color="#660033" face="calibri">
'. implode('</td><td bgcolor="#FFFFDF"><font color="#660033" face="calibri">',$row) .'
</td><td bgcolor="#FFFFDF"><form action="./budget.php"><input type="hidden" name="delete" value="1">
<input type="hidden" name="id" value="???????"><center><input type="submit" value="Delete"></center></form></td></tr>';
                        $colorvar = 2;
                } else
                {
                        echo '<tr><td><font color="#660033" face="calibri">
'. implode('</td><td><font color="#660033" face="calibri">',$row) .'
</td><td><form action="./budget.php"><input type="hidden" name="delete" value="1">
<input type="hidden" name="id" value="???????????">
<center><input type="submit" value="Delete"></center></form></td></tr>';
                        $colorvar = 1;
                }
 
                $resultcount = $resultcount + 1;
        }while( $row = mysql_fetch_assoc($result) );

Open in new window

0
Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

 
LVL 60

Expert Comment

by:Kevin Cross
ID: 22642855
$row[0] should get you column or current row where 0 is the index of the column.  Can also use $row["id"] if I am not mistaken.

Here is some more information on implode syntax: http://us3.php.net/implode.

Thought you indicated the code you had already worked to display data on the page though.  Anyway, once you get the id in the HTML page source, you can use what I posted earlier to retrieve the input field with name="id" for use in the db DELETE query.
0
 

Author Comment

by:runelynx
ID: 22642871
When I try to use $row, $row[0], or $row[0][0] ...  all three return  the string   "Array"

Help? :(
0
 

Author Comment

by:runelynx
ID: 22642880
when I do

print_r ($row);

I get:

Array (     [id] => 22     [active] =>      [type] => Test     [main] => One     [secondary] => Two     [weekly] =>      [monthly] =>  ) Array (     [id] => 18     [active] =>      [type] => Create     [main] => One     [secondary] => Entry     [weekly] =>      [monthly] =>  ) Array (     [id] => 20     [active] =>      [type] =>      [main] =>      [secondary] =>      [weekly] =>      [monthly] =>  )
0
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 22642897
Hopefully some more experienced PHP folks will chime in.  I am more of SQL Syntax person and as I stated I thought you had this part working and just needed the next step to delete.

It looks like $row is the array being used for implode.  Is $result the actual record from mysql.  If so, that is the variable you can use for $result["id"] or $result[0] (note you must replace 0 with actual column index of id -- if it is the first column, then 0 is fine).
0
 
LVL 60

Accepted Solution

by:
Kevin Cross earned 500 total points
ID: 22642903
Alright look here: http://us2.php.net/array.

Use $row['id'].
0
 

Author Comment

by:runelynx
ID: 22642914
Ok got it to work... using:

'.  $row["id"]   .'

for some reason the double quotes vs single quotesi nside the brackets made a difference
0
 
LVL 60

Expert Comment

by:Kevin Cross
ID: 22642922
Glad that helped.  Sorry I misunderstood initially.

Good luck.

Regards,
Kevin
0

Featured Post

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
MySQL Persistent Connections 10 36
Html split(text) 2 32
Session on Html 8 42
mysql vs miscrosoft sql server 6 20
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question