help on Automatic code generation...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
vsrivastava21
Forum Newbie
Posts: 1
Joined: Fri Aug 15, 2008 1:07 pm

help on Automatic code generation...

Post by vsrivastava21 »

I have created a function to create an automatic code i.e. like employee code, customer code or seeker code etc. I have some problem while writing code , please help somebody...

I also fetching the values from database to generate code.

Please help me out... its urgent...

regards
Vivek

<?php
$fn=$_POST["status"]

function newCode($fn)
{
$db="scientist";
$link=mysql_connect("localhost","root","admin");
if(!$link)
die("could not connect to mysql".mysql_error());
mysql_select_db($db,$link) or die("could not connect to $db".mysql_error());
$result=mysql_query("select * from code_generate where field_name='$fn'");
while($row=mysql_fetch_array($result))
{
$autoGen=$row[1];
echo $autoGen;
if($autoGen.equals("N"))
{
$newcode="" ;
}
else
{
$fp=$row[2]; //fetching prefix from table.
$tp=(String)$row[4]; //fetching current_no from table.
$tempCode=$fp+$tp;
$num=(int)$row[3]-strlen($tempCode); //fetching length from table.
$temp="000000000000";
$sp=temp.substr(0,$num);
$newcode=$fp+$sp+$tp;
echo $newCode;
}
}
return $newcode;
}
$code=newCode($fn);
?>
desmi
Forum Commoner
Posts: 64
Joined: Sun Jun 15, 2008 4:55 am

Re: help on Automatic code generation...

Post by desmi »

First of all, please use

Code: Select all

or

Code: Select all

tags when posting your code, if its php code, prefer

Code: Select all

tags..
desmi
Forum Commoner
Posts: 64
Joined: Sun Jun 15, 2008 4:55 am

Re: help on Automatic code generation...

Post by desmi »

And then, your code:

Code: Select all

 
<?php
$fn=$_POST["status"] // You're missing ; here, try after adding it
 
function newCode($fn)
{
$db="scientist";
$link=mysql_connect("localhost","root","admin");
if(!$link)
die("could not connect to mysql".mysql_error());
mysql_select_db($db,$link) or die("could not connect to $db".mysql_error());
$result=mysql_query("select * from code_generate where field_name='$fn'");
while($row=mysql_fetch_array($result))
{
$autoGen=$row[1];
echo $autoGen;
if($autoGen.equals("N"))
{
$newcode="" ;
}
else
{
$fp=$row[2]; //fetching prefix from table.
$tp=(String)$row[4]; //fetching current_no from table.
$tempCode=$fp+$tp;
$num=(int)$row[3]-strlen($tempCode); //fetching length from table.
$temp="000000000000";
$sp=temp.substr(0,$num);
$newcode=$fp+$sp+$tp;
echo $newCode;
}
}
return $newcode;
}
$code=newCode($fn);
?>
 
And when added {} to your first if and run through beautifier:

Code: Select all

 
<?php
$fn = $_POST["status"];
 
function newCode($fn)
{
    $db = "scientist";
    $link = mysql_connect("localhost", "root", "admin");
    if (!$link) {
        die("could not connect to mysql" . mysql_error());
    }
    mysql_select_db($db, $link) or die("could not connect to $db" . mysql_error());
    $result = mysql_query("select * from code_generate where field_name='$fn'");
    while ($row = mysql_fetch_array($result)) {
        $autoGen = $row[1];
        echo $autoGen;
        if ($autoGen . equals("N")) {
            $newcode = "";
        } else {
            $fp = $row[2]; //fetching prefix from table.
            $tp = (string )$row[4]; //fetching current_no from table.
            $tempCode = $fp + $tp;
            $num = (int)$row[3] - strlen($tempCode); //fetching length from table.
            $temp = "000000000000";
            $sp = temp . substr(0, $num);
            $newcode = $fp + $sp + $tp;
            echo $newCode;
        }
    }
    return $newcode;
}
$code = newCode($fn);
?>
 
Post Reply