Jump to content

Adding a function


hewstone999

Recommended Posts

I have some code below which outputs a HTML table. The values in each cell come from a database. I want to add a function to the view and delete cell (marked in red) so when everytime a user clicks on it, it will do a SQL command i.e. "Delete * From ".$Dis.    How can i do this??

 

$temp = 1;

while(!$rs->EOF){

$date = $rs->Fields("date1")->value;

$time = $rs->Fields("Time1")->value;

$Dis = $rs->Fields("table_name")->value;

 

 

if ($temp1 == 1){

echo "<tr>";

echo "<td><font size='-1'><b>Date</b>";

echo "</td>";

echo "<td><font size='-1'><b>Time</b>";

echo "</td>";

echo "<td><font size='-1'><b>TableName</b>";

echo "</td>";

echo "</tr>";

}

echo "<tr>";

echo "<td><font size='-1'>".$date;

echo "</td>";

echo "<td><font size='-1'>".$time;

echo "</td>";

echo "<td><font size='-1'>".$Dis;

echo "</td>";

echo "<td><font size='-1'>View";

echo "</td>";

echo "<td><font size='-1'>Delete";

echo "</td>";

echo "</tr>";

 

$temp = $temp + 1;

 

$rs->MoveNext();

Link to comment
https://forums.phpfreaks.com/topic/152039-adding-a-function/
Share on other sites

I have some code below which outputs a HTML table. The values in each cell come from a database. I want to add a function to the view and delete cell (marked in red) so when everytime a user clicks on it, it will do a SQL command i.e. "Delete * From ".$Dis.    How can i do this??

 

$temp = 1;

while(!$rs->EOF){

$date = $rs->Fields("date1")->value;

$time = $rs->Fields("Time1")->value;

$Dis = $rs->Fields("table_name")->value;

 

 

if ($temp1 == 1){

echo "<tr>";

echo "<td><font size='-1'><b>Date</b>";

echo "</td>";

echo "<td><font size='-1'><b>Time</b>";

echo "</td>";

echo "<td><font size='-1'><b>TableName</b>";

echo "</td>";

echo "</tr>";

}

echo "<tr>";

echo "<td><font size='-1'>".$date;

echo "</td>";

echo "<td><font size='-1'>".$time;

echo "</td>";

echo "<td><font size='-1'>".$Dis;

echo "</td>";

echo "<td><font size='-1'>View";

echo "</td>";

echo "<td><font size='-1'>Delete";

echo "</td>";

echo "</tr>";

 

$temp = $temp + 1;

 

$rs->MoveNext();

 

ok lets go over it alitle

 

 

first you can save space by changing $temp = $temp + 1; to->  $temp++;

 

now... lets start by checking for $_GET Vars

 

////add a condition before the while loop to check for $_GET vars and set the variables...

 

$action = (!empty($_GET['action'])?$_GET['action']:'');

$id = (!empty($_GET['id'])?$_GET['id']:'');

 

 

///Now check to see if they both exist

/// no point to have action without id

if (!empty($action) && !empty($id))

{

/// a switch to handle action

/// you can add more actions to the switch such as set_inactive or disable / enable...

switch($action)

{

case 'view':

{

///SELECT QUERY HERE

$query = 'SELECT * FORM `table` WHERE `id`='.$id.' LIMIT 1';

$result = mysql_query($query);

                               

if ($result)

{

                                      while ($row = mysql_fetch_assoc($result) )

{

///echo 'TABLE INFORMATION or what ever you want to show';

echo 'Date: ' . $row['date'].'<br>';

echo 'name: ' . $row['name'].'<br>';

///and so on and on

}

}

else

{

echo 'Could not get data';

}

break;

}

case 'delete':

{

///DELETE QUERY HERE

$query = 'DELETE FORM `table` WHERE `id`='.$id.' LIMIT 1';

$result = mysql_query($query);

if ($result)

{

echo 'Record deleted sussesfuly';

}

else

{

echo 'Could not delete data';

}

break;

break;

}

default:

break;

}

}

 

$temp = 1;

while(!$rs->EOF)

{

//// now here you will have to add the value to build the link for the row you want to view or delete

  $id = $rs->Fields("id")->value;

  $date = $rs->Fields("date1")->value;

  $time = $rs->Fields("Time1")->value;

  $Dis = $rs->Fields("table_name")->value;

 

 

  if ($temp1 == 1){

  echo "<tr>";

      echo "<td><font size='-1'><b>Date</b>";

      echo "</td>";

      echo "<td><font size='-1'><b>Time</b>";

      echo "</td>";

      echo "<td><font size='-1'><b>TableName</b>";

      echo "</td>";

  echo "</tr>";

  }

  echo "<tr>";

      echo "<td><font size='-1'>".$date;

      echo "</td>";

      echo "<td><font size='-1'>".$time;

      echo "</td>";

      echo "<td><font size='-1'>".$Dis;

      echo "</td>";

      echo '<td><font size="-1"><a href="'.$_SERVER['PHP_SELF'].'?id='.$id.'&action=view">View</a>';

      echo "</td>";

      echo '<td><font size="-1"><a href="'.$_SERVER['PHP_SELF'].'?id='.$id.'&action=delete">Delete</a>';

      echo "</td>";

  echo "</tr>";

 

$temp++;

 

  $rs->MoveNext();

 

}

 

Let me know how it works out for you...

 

if you need more help i'm here sometimes, but there are more people here.

so one of us will help...

Good luck

Link to comment
https://forums.phpfreaks.com/topic/152039-adding-a-function/#findComment-798523
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.