Log in

View Full Version : looping in PHP n MySql



w3bdiv3r
08-25-2009, 05:00 AM
Hi bro..
anw i've a looping problem in string + integer(e.g PP001)


else if($_GET[act]=='viewProgressProject')
{
include "connect.php";
$sql = mysql_query("select * from progress where IDPro = '$_GET[id]'");

while($fetch = mysql_fetch_array($sql))
{
if ($zzz = substr($fetch[0],2,3) + 1)
{
$test = "PP00";
$gg = $test.$zzz;
}
else if($zzz = substr($fetch[0],2,3) + 10)
{
$test = "PP0";
$gg = $test.$zzz;
}
else
{
$test = "PP";
$gg = $test.$zzz;
}
}

echo "
<h2>Progress Project : $_GET[id]</h2>
<form id=formViewProgressProject name=formViewProgressProject method=post action='action.php?module=project&act=doUpdateProgressProject'>
<table>
<tr>
<td>ID Progress</td>
<td> : <input type=text name=txtIDProgress id=txtIDProgress value='$gg' readonly=readonly/></td>
</tr>

prasanthmj
08-25-2009, 07:02 AM
What is the problem/output that you are getting?

It should be


intval(substr($fetch[0],2,3)) + 10

I guess?

Also in the else case, $zzz is not initialized?

w3bdiv3r
08-25-2009, 07:31 AM
according to my code, my looping results are PP001,PP002,PP003......,PP0010,PP0011, whereas i want my results are PP010,PP011 not PP0010,PP0011..
u got my questions?

JShor
08-25-2009, 04:49 PM
See if this fixes it for you:



else if($_GET[act]=='viewProgressProject')
{
include "connect.php";
$sql = mysql_query("select * from progress where IDPro = '$_GET[id]'");

while($fetch = mysql_fetch_array($sql))
{
if ($zzz = substr($fetch[0],2,3) + 1)
{
$test = "PP00";
$gg = $test.$zzz;
}
else if($zzz = substr($fetch[0],2,3) + 10)
{
$test = "PP0";
$gg = $test.$zzz;
}
else
{
$test = "PP";
$gg = $test.$zzz;
}
}

str_replace("00", "0", $test);

echo "
<h2>Progress Project : $_GET[id]</h2>
<form id=formViewProgressProject name=formViewProgressProject method=post action='action.php?module=project&act=doUpdateProgressProject'>
<table>
<tr>
<td>ID Progress</td>
<td> : <input type=text name=txtIDProgress id=txtIDProgress value='$gg' readonly=readonly/></td>
</tr>

w3bdiv3r
09-10-2009, 03:35 AM
@prasanthmj : my looping results are PP001,PP002,PP003......,PP0010,PP0011, whereas i want my results are PP010,PP011 not PP0010,PP0011..
anw thx for ur help..

@JShor : hmm.. ur code results are same with my code results..>.<
anw thx for ur help..

anybody could help me to solve this problem??

forum_amnesiac
09-10-2009, 06:33 AM
Have you tried using strlen(), ie


if (strlen(trim($zzz)) == 1)
{
$test = "PP00";
$gg = $test.$zzz;
}
else if(strlen(trim($zzz)) == 2)
{
$test = "PP0";
$gg = $test.$zzz;
}
else
{
$test = "PP";
$gg = $test.$zzz;
}