Friday 22 April 2011

zpanel module making working with daterbase and functions

Advance module making.Working with zpanel daterbase and functions

ok so now we are going to learn how to use the zpanel datebase and how to link tables to gether to get the final result. We are first going to display

Hello zadmin. We just found out that your full name is john blogs



copy and past the code below into the index.php file or download code here or just follow along by looking at the picture below.

<?php
include('conf/zcnf.php'); // connection details for zpanel daterbase
$username = $_SESSION['zUsername']; // take the username
mysql_select_db($z_db_name, $zdb);// select zpanel daterbase
#####################################################################
// find users id
$query = "SELECT * FROM z_accounts WHERE ac_user_vc='$username'";
$name = mysql_query($query, $zdb) or die(mysql_error());
$row_name = mysql_fetch_assoc($name);
$userid = $row_name['ac_id_pk']; // use's id
// testing echo $userid;
#######################################################################
//find user's name
$query_name = "SELECT * FROM z_personal WHERE ap_id_pk='$userid'";
$name_fianl = mysql_query($query_name, $zdb) or die(mysql_error());
$row_name_fianl = mysql_fetch_assoc($name_fianl);
$user_full_name = $row_name_fianl['ap_fullname_vc']; // user's full name
// testing echo $user_full_name;
#######################################################################
// show the message
$start = 'Hello ' . $username;
$middle = '. We just found out that your full name is ';
$full_message = $start . $middle . $user_full_name;
echo $full_message;
?>



so as you should see and undertand i am make two sql querys here.

The first one is to find out the user's id from there username supplied by the session varable zUsername. We will find the users id by looking in the zpanel daterbase and looking under the table z_accounts
but becuase the users full name is not in this table we just find the users id
The second and final stage is to take that user's id to find the user's fullname in the table z_personal.

i hope this tuerial has shown you how to link zpanel table to get the final product.

but as you can see there is quite a bit of code there to find something so small so there are two things we can do to make it smaller!

make a where sql daterbase function

user a different session

use both above to make it tiny :D





make a function so first in your module directory make a folder called functions and then in that fresh folder make a file called daterbase.php. in that file copy and post this code and save or download load code here



<?php
function daterbase($for_name,$table,$where,$where_object,$row,$zdb){
$query = "SELECT * FROM $table WHERE $where='$where_object'";
$name = mysql_query($query, $zdb) or die(mysql_error());
$row_name = mysql_fetch_assoc($name);
$for_name = $row_name[$row];
return $for_name;
}
?>


now we need to edit the index file to use this function instead of keep repleating it over and over gain.
rmember we have to include the function file and remeber the rule with the link
or if you want copy and past thi code into the index file

<?php
include('conf/zcnf.php'); // connection details for zpanel daterbase
$username = $_SESSION['zUsername']; // take the username
include('modules/account/hello/functions/daterbase.php');
mysql_select_db($z_db_name, $zdb);
#####################################################################
// find users id
$userid = daterbase('userid','z_accounts','ac_user_vc',$username,'ac_id_pk',$zdb);
#######################################################################
//find user's name
$user_full_name = daterbase('userid','z_personal','ap_id_pk',$userid,'ap_fullname_vc',$zdb);
#######################################################################
// show the message
$start = 'Hello ' . $username . '. We just found out that your full name is ';
echo $start . $user_full_name;
?>




 now all i have done here is used are function instead of typing out all that mysql query stuff. i have included the function file. and made the message
code slightly smaller.
  1. uses function check
  2. now we can use a another session that is set on login that will make this even shorter and that session is $_SESSION['zUserID'] ye the users id
so lets still use the function but we dont need to find user id any more as we all ready have it here $_SESSION['zUserID']
so all you have to do is change the varable $userid

from
$userid = daterbase('userid','z_accounts','ac_user_vc',$username,'ac_id_pk',$zdb);
to
$userid = $_SESSION['zUserID'];
using correct session check
download the final code here!

No comments:

Post a Comment

i do all this for free so PLEASE NOT SPAM thanks