How do I go about

... outputting 3 certain columns of data from a table...

Page 1 of 1

1 Replies - 819 Views - Last Post: 07 December 2009 - 10:33 PM Rate Topic: -----

#1 DeWire  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 73
  • Joined: 06-December 08

How do I go about

Posted 07 December 2009 - 02:05 PM

Hi lads,

I have a table with userID, topicID, topicName, details, time and message.

I want to output topicID, userID and topicName to a table using a query.
With this data, I want to be able to click on the topicName, and on clicking it, delete it and its related data from the table.

How could I go about doing this ? I have so far got the table coming up with the topicID, userID and topicName data. But I do not have a clue how to add the link to the topicName so that when it is clicked it is deleted ( along with all its related data)

All help would be much appreciated.

This is what I have so far:
<?php

  session_start();

  // Check for an authenticated session variable
  // Output a permission denied message, if the session variable is not set
  // Output the "protected" content to authenticated members



 if (! isset($_SESSION['authenticated']))
	{
	  header ("Location:login.php");

	}
?>


<html>
<head><title>Forum</title>
		<link rel="stylesheet" type="text/css" href="inc/style.css">
</head>
<body>
<?php
include("inc/header.php");
ERROR_REPORTING(E_ALL);
ini_set('display_errors','On');


include("db_connect.php");
$db_link = db_connect("forumDOD");

$fields = mysql_list_fields("forumDOD", "topic");
$num_columns = mysql_num_fields($fields);
?>
<div id="content"><div align="center"><h1>Delete Your Topics</h1></div>

<?php
$query = "SELECT topicID,userID,topicName FROM topic";
$result = mysql_query($query) or die("SQL query failed");

echo '<table border="1" width="500" align="center">', "\n";

echo "<tr>\n";
for ($i = 0; $i < 3; $i++)
{
   echo "<th>", mysql_field_name($fields, $i), "</th>\n";
}
echo "</tr>\n";

while ($row = mysql_fetch_assoc($result))
{
	$col_value= 3;
   echo "<tr>\n";
   foreach ($row as $col_value)
   {
	  echo "<td>$col_value</td>\n";
   }
   echo "</tr>\n";
}

echo "</table>\n";

mysql_free_result($result);
mysql_close($db_link);
include("inc/footer.php");
?></div>
</body>
</html>




Thanks,
Dave

Is This A Good Question/Topic? 0
  • +

Replies To: How do I go about

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 5121
  • View blog
  • Posts: 13,790
  • Joined: 18-April 07

Re: How do I go about

Posted 07 December 2009 - 10:33 PM

Well instead of looping through the columns dynamically like you are, you will need to print the columns individually for each row. That way you can put a link onto the topicName field...

while ($row = mysql_fetch_assoc($result))
{
   echo "<tr>\n";
   echo "<td>{$row["topicID"]}</td>";
   echo "<td>{$row["userID"]}</td>";
   echo "<td>{$row["topicName"]}</td>";
   echo "</tr>\n";
}



Now I suggest you also query the row's primary key (here I call it ID) that will be used to locate the record for deletion. After you have each row broken out into its cells, you can go about making topicName a link to the current page...

while ($row = mysql_fetch_assoc($result))
{
   echo "<tr>\n";
   echo "<td>{$row["topicID"]}</td>";
   echo "<td>{$row["userID"]}</td>";
   echo "<td><a href=\"currentpage.php?delid={$row["id"]}\">{$row["topicName"]}</a></td>";
   echo "</tr>\n";
}



This will give you a link on the topicName field which should point to the CURRENT file and pass it a parameter called "delid" in which you give it the row's primary key. Now at the top of this file you are going to check for that parameter, check its type and range, if it is coming from the right source etc.... and use it to build a delete query which will delete the row. Then when the rest of the page after that loads, the row is magically gone from the display and from your database.

Hope you get what I am saying here. :)
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1