PDA

View Full Version : handling class and function



mrjameer
01-23-2007, 07:14 PM
hi

i have a script for displaying the records from mysql table along with checkboxes to delete the multiple records from table.what i want is i want to put the code inside one class and one function.that's all.but when i put the code in class & function the record is not deleting but when i remove the class & function the record is deleting.any of your help will be appreciated.
here is my code
view.html.php


<?php
//class viewclients_html
//{
//function viewclients()
//{
include 'db.php';
mysql_connect($host,$username,$password);
mysql_select_db($db) or die("Unable to select database");

$sql="SELECT * FROM host ORDER BY date DESC";
$result=mysql_query($sql);

$count=mysql_num_rows($result);

?>
<center><table width="700" border="0" cellspacing="1" cellpadding="0">
<tr>
<td><font color="navy"><form name="form1" method="post" action=""><br><center><b>
<u><font color="magenta">CLIENTS</font></u></b></center><br><br>
<table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr>

<td bgcolor="lightgreen" align="center"><font color="navy">#</font></td>
<td bgcolor="lightgreen" align="center"><font color="navy"><strong>Id</strong></font></td>
<td bgcolor="lightgreen" align="center"><font color="navy"><strong>Name</strong></font></td>
<td bgcolor="lightgreen" align="center"><font color="navy"><strong>Email</strong></font></td>
<td bgcolor="lightgreen" align="center"><font color="navy"><strong>Phone</strong></font></td>
<td bgcolor="lightgreen" align="center"><font color="navy"><strong>Country</strong></font></td>
<td bgcolor="lightgreen" align="center"><font color="navy"><strong>City</strong></font></td>
<td bgcolor="lightgreen" align="center"><font color="navy"><strong>Message</strong></font></td>
<td bgcolor="lightgreen" align="center"><font color="navy"><strong>Date</strong></font></td>

</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>
<font color="navy">
<tr class="odd" onmouseover="over(this);" onmouseout="out(this);">
<td align="center"><font color="navy"><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $rows['id']; ?>"></font></td>
<td><font color="navy"><? echo $rows['id']; ?></font></td>


<td><font color="navy"><? echo $rows['name']; ?></font></td>
<td><font color="navy"><? echo $rows['email']; ?></font></td>
<td><font color="navy"><? echo $rows['phone']; ?></font></td>
<td><font color="navy"><? echo $rows['country']; ?></font></td>
<td><font color="navy"><? echo $rows['city']; ?></font></td>
<td><font color="navy"><? echo $rows['message']; ?></font></td>
<td><font color="navy"><? echo $rows['date']; ?></font></td>
</tr></font>
<?php
}
?>
<tr>
<td colspan="5" align="center" ><input name="delete" type="submit" id="delete" value="Delete"></font></td>
</tr></table></center>
<?
// Check if delete button active, start this
if($delete){
for($i=0;$i<$count;$i++){
$del_id = $checkbox[$i];
$sql = "DELETE FROM host WHERE id='$del_id'";
$result = mysql_query($sql);
}

// if successful redirect to checkdelete1.php
if($result){
echo "<meta http-equiv=\"refresh\" content=\"0;URL=viewclients.php\">";
}
}
mysql_close();
//}} class & function close
?>
</table>
</form>
</font></td>
</tr>
</table></center>
<style type="text/css">
<!--
tr.odd{background-color:white;}
tr.even{background-color:red;}
-->
</style>
<script language="JavaScript" type="text/javascript">
<!--
function over(row)
{
row.oldBackgroundColor = row.style.backgroundColor;
row.style.backgroundColor = 'pink';
}
function out(row)
{
row.style.backgroundColor = row.oldBackgroundColor;
}
//-->
</script>


iam accessing the function in another script as follows
view.php
<?php
require_once( $mainframe->getPath( 'front_html' ) );

viewclients_HTML::viewclients();
?>

gunman
01-24-2007, 09:03 AM
Try it like this



<?php
class viewclients_html
{
function viewclients()
{
include 'db.php';
mysql_connect($host,$username,$password);
mysql_select_db($db) or die("Unable to select database");

$sql="SELECT * FROM host ORDER BY date DESC";
$result=mysql_query($sql);

$count=mysql_num_rows($result);

?>
<center><table width="700" border="0" cellspacing="1" cellpadding="0">
<tr>
<td><font color="navy"><form name="form1" method="post" action=""><br><center><b>
<u><font color="magenta">CLIENTS</font></u></b></center><br><br>
<table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr>

<td bgcolor="lightgreen" align="center"><font color="navy">#</font></td>
<td bgcolor="lightgreen" align="center"><font color="navy"><strong>Id</strong></font></td>
<td bgcolor="lightgreen" align="center"><font color="navy"><strong>Name</strong></font></td>
<td bgcolor="lightgreen" align="center"><font color="navy"><strong>Email</strong></font></td>
<td bgcolor="lightgreen" align="center"><font color="navy"><strong>Phone</strong></font></td>
<td bgcolor="lightgreen" align="center"><font color="navy"><strong>Country</strong></font></td>
<td bgcolor="lightgreen" align="center"><font color="navy"><strong>City</strong></font></td>
<td bgcolor="lightgreen" align="center"><font color="navy"><strong>Message</strong></font></td>
<td bgcolor="lightgreen" align="center"><font color="navy"><strong>Date</strong></font></td>

</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>
<font color="navy">
<tr class="odd" onmouseover="over(this);" onmouseout="out(this);">
<td align="center"><font color="navy"><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $rows['id']; ?>"></font></td>
<td><font color="navy"><? echo $rows['id']; ?></font></td>


<td><font color="navy"><? echo $rows['name']; ?></font></td>
<td><font color="navy"><? echo $rows['email']; ?></font></td>
<td><font color="navy"><? echo $rows['phone']; ?></font></td>
<td><font color="navy"><? echo $rows['country']; ?></font></td>
<td><font color="navy"><? echo $rows['city']; ?></font></td>
<td><font color="navy"><? echo $rows['message']; ?></font></td>
<td><font color="navy"><? echo $rows['date']; ?></font></td>
</tr></font>
<?php
}
?>
<tr>
<td colspan="5" align="center" ><input name="delete" type="submit" id="delete" value="Delete"></font></td>
</tr></table></center>
<?
// Check if delete button active, start this
if($delete){
for($i=0;$i<$count;$i++){
$del_id = $checkbox[$i];
$sql = "DELETE FROM host WHERE id='$del_id'";
$result = mysql_query($sql);
}

// if successful redirect to checkdelete1.php
if($result){
echo "<meta http-equiv=\"refresh\" content=\"0;URL=viewclients.php\">";
}
}
mysql_close();
//}} class & function close
?>
</table>
</form>
</font></td>
</tr>
</table></center>
<style type="text/css">
<!--
tr.odd{background-color:white;}
tr.even{background-color:red;}
-->
</style>
<script language="JavaScript" type="text/javascript">
<!--
function over(row)
{
row.oldBackgroundColor = row.style.backgroundColor;
row.style.backgroundColor = 'pink';
}
function out(row)
{
row.style.backgroundColor = row.oldBackgroundColor;
}
//-->
</script>
<?

}

}
?>


And you could call it like



<?php
require_once( $mainframe->getPath( 'front_html' ) );
$cl = new(viewclients_html);
$cl->viewclients();
?>

mrjameer
01-25-2007, 03:28 PM
hi gunman,


thanks for your reply.i tried your code but the record is not deleted.any more suggestions you have, please post it.


thanks
mrjameer