Category Archives: php

Edit data in php

php mysql basic step by step tutorial -update record: In editing the data, you can use the syntax as follow:

file: db.inc.php for connection database

<?
//file include for database connection
//db.inc.php
//database connection
mysql_connect(“localhost”,”root”,”admin”);

// database selection
mysql_select_db(“employees”);
?>

file: edit.php for connection database

<html>
<head>
<title>Untitled Document</title>
<meta http-equiv=”Content-Type” content=”text/html; charset=iso-8859-1″>
</head>
<body>
<table>
<tr>
<td align=”center”>EDIT DATA</td>
</tr>
<tr>
<td>
<table border=”1″>
<?
include”db.inc.php”;//database connection
$order = “SELECT * FROM data_employees”;
$result = mysql_query($order);
while ($row=mysql_fetch_array($result)){
echo (“<tr><td>$row[name]</td>”);
echo (“<td>$row[employees_number]</td>”);
echo (“<td>$row[address]</td>”);
echo (“<td><a href=\”edit_form.php?id=$row[employees_number]\”>Edit</a></td></tr>”);
}
?>
</table>
</td>
</tr>
</table>
</body>
</html>

The picture above is edit.php file where this file will show overall data in the table, then there is edit menu in the last column. If you click the edit menu, it will bring the program to execute edit_form.php file. Edit_form.php file will show a form to edit the data which have been selected in the previous form. The mechanism is that the user choose one of the data that will be edited in the first form (edit.php file) by clicking the edit menu in the right column. You can see edit_form.php program file as follow:

<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN”
http://www.w3.org/TR/html4/loose.dtd”&gt;
<html>
<head>
<title>Form Edit Data</title>
</head>

<body>
<table border=1>
<tr>
<td align=center>Form Edit Employees Data</td>
</tr>
<tr>
<td>
<table>
<?
include “db.inc.php”;//database connection
$order = “SELECT * FROM data_employees
where employees_number=’$id'”;
$result = mysql_query($order);
$row = mysql_fetch_array($result);
?>
<form method=”post” action=”edit_data.php”>
<input type=”hidden” name=”id” value=”<? echo “$row[employees_number]”?>”>
<tr>
<td>Name</td>
<td>
<input type=”text” name=”name”
size=”20″ value=”<? echo “$row[name]”?>”>
</td>
</tr>
<tr>
<td>Address</td>
<td>
<input type=”text” name=”address” size=”40″
value=”<? echo “$row[address]”?>”>
</td>
</tr>
<tr>
<td align=”right”>
<input type=”submit”
name=”submit value” value=”Edit”>
</td>
</tr>
</form>
</table>
</td>
</tr>
</table>
</body>
</html>

By clicking the edit button, the program goes to the fourth program, edit_data.php file, which brings three variable such as $id variable which contains of employees number data, $name variable which contains of employees name data, and $address variable which contains of employees address. In order to know whether the data is already change or not, the program is re-instructed to edit.php file with the order of header (“location:edit.php”). Here is the edit_data.php program file:

<?
//edit_data.php
include “db.inc.php”;
$order = “UPDATE data_employees
SET name=’$name’,
address=’$address’
WHERE
employees_number=’$id'”;
mysql_query($order);
header(“location:edit.php”);
?>

Advertisements

How can show tree list (recursive) in joomla for custome component?

1. First in Your component module class with function

function getTotal()
{
if (empty($this->_total))
{
$query = $this->_buildQuery();
$this->_total = $this->_getListCount($query);
}
return $this->_total;
}

replace with function
function getData()
{
if (empty($this->_data))
{
$query = $this->_buildQuery();
$this->_data = $this->_getList($query);
$children = array ();
// first pass – collect children
foreach ( $this->_data as $v ) {
$pt = $v->parent_id;
$list = @$children [$pt] ? $children [$pt] : array ();
array_push ( $list, $v );
$children [$pt] = $list;
}
// second pass – get an indent list of the items
$list = JHTML::_ ( ‘menu.treerecurse’, 0, ”, array (), $children, 9999, 0, 0 );
$this->_data = array_slice ( $list, $this->getState(‘limitstart’), $this->getState(‘limit’));
}
return $this->_data;
}

2. In View (folder) Your default view listing
for ($i=0, $n=count( $this->items ); $i items[$i];
//category name print like
treename ; ?>
}

3. In Your Form view

add code for recursive function like for example

$value)
{
$stateslist1[] = JHTML::_(‘select.option’, $key, $value, ‘id’, ‘title’ );
}
$stateslist2 = array_merge( $stateslist,$stateslist1 );
$lists[‘id’] = JHTML::_(‘select.genericlist’, $stateslist2, ‘parent_id’, ‘class=”inputbox” size=”1″ style=”width:200px;”‘,’id’, ‘title’, $this->category->parent_id );

echo $lists[‘id’];

function getTree($id=0)
{
static $cates = array();
static $times = 0;
$times++;
$db =& JFactory::getDBO();
$query = ‘SELECT * FROM #__dir_categories where parent_id=’.$id;
$db->setQuery( $query );
$rowdata = $db->loadObjectList();
foreach($rowdata as $val)
{
$cates[$val->id] = str_repeat(“… “,$times-1).$val->title;
getTree($val->id);
}
$times=$times-1;
return $cates;
}
?>

How can write insert, update and delete query in joomla

for insert

$db            = & JFactory::getDBO();

$ins = new stdClass();
$ins->id = NULL;  // here id is auto increment id and primary key

$ins->table_field =$post[‘table_field’]; // table field name

…………………..

……………………..

if($db->insertObject(“jos_tablename”, $ins, ‘id’)){
unset($db);
}else{
echo $db->stderr();
}

for update

$ins->id = NULL;  // here id is  post/get retrieve value

$ins->table_field =$post[‘table_field’]; // table field name

…………………..

……………………..

if($db->updateObject(“jos_tablename”, $ins, ‘id’)){
unset($db);
}else{
echo $db->stderr();
}

foe delete

$db       =& JFactory::getDBO();
$query = ‘DELETE FROM jos_tablename’;
$db->setQuery( $query );
$db->query();

How can apply custom menu with different css?

Go to dashboard ->appearance->menus
now click on Screen Options right top corner and check on CSS Classes.
When create a menu than add a css class for individual menu.

add a php code in your header.php

<nav id=”access” role=”navigation”>
<?php wp_nav_menu( array( ‘menu’ => ‘custom_menu’ ) ); ?>
</nav>

and apply a css style.css
#access li.home {background:url(“images/green.jpg”) no-repeat 0 1px transparent;width:116px; height:37; text-align:center; vertical-align:middle;}
#access li.about {background:url(“images/blue.png”) no-repeat 0 1px transparent;width:107px; height:37; text-align:center; vertical-align:middle;}
#access li.project {background:url(“images/orange.png”) no-repeat 0 1px transparent;width:105px; height:37; text-align:center; vertical-align:middle;}
#access li.contact {background:url(“images/red.png”) no-repeat 0 1px transparent;width:105px; height:37; text-align:center; vertical-align:middle;}

#access {
clear: both;
display: block;
float: right;
margin: 0;

}
#access ul {
font-family:Arial; font-size: 10pt;
list-style: none;
margin: 0 0 0 -0.8125em;

padding-left: 0;
}
#access li {
float: left;
position: relative;
font-family:Arial; font-size: 10pt;
}
#access a {
color: #FFF;
display: block;
line-height: 37px;
padding:0;
text-decoration: none;
}

#access ul ul {
display: none;
float: left;
margin: 0;
position: absolute;
top: 37px;
left: 0;
width: auto;
z-index: 99999;
}
#access ul ul ul {
left: 100%;
top: 0;
}
#access ul ul li.aboutul{
background: #00A9C8;
width:125px;
}

#access ul ul li.projectli{
background: #F5BB01;
width:120px;
}
#access ul ul li.contactli{
background: #F54E00;
width:110px;
}

#access ul ul a{

color: #444;
font-family:Arial; font-size: 9pt;
font-weight: normal;
height: auto;
line-height: 15px;
padding: 5px 0 10px 0;

}
#access li:hover > a,
#access ul ul :hover > a,
#access a:focus {
color: #000000;
}
#access li:hover > a,
#access a:focus {
color: #000000;
}
#access ul li:hover > ul {
display: block;
}
#access .current-menu-item > a,
#access .current-menu-ancestor > a,
#access .current_page_item > a,
#access .current_page_ancestor > a {
font-weight: bold;
}

.assistive-text {
position: absolute !important;
clip: rect(1px 1px 1px 1px); /* IE6, IE7 */
clip: rect(1px, 1px, 1px, 1px);
}
#access a.assistive-text:active,
#access a.assistive-text:focus {
background: #eee;
border-bottom: 1px solid #ddd;
color: #1982d1;
clip: auto !important;
font-size: 12px;
position: absolute;
text-decoration: underline;
top: 0;
left: 7.6%;
}