Results 1 to 6 of 6

Thread: looping in PHP n MySql

  1. #1
    Join Date
    Aug 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default looping in PHP n MySql

    Hi bro..
    anw i've a looping problem in string + integer(e.g PP001)

    Code:
    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>

  2. #2
    Join Date
    Nov 2008
    Posts
    58
    Thanks
    0
    Thanked 7 Times in 7 Posts

    Default

    What is the problem/output that you are getting?

    It should be
    Code:
    intval(substr($fetch[0],2,3)) + 10
    I guess?

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

  3. #3
    Join Date
    Aug 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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?

  4. #4
    Join Date
    Mar 2007
    Location
    New York, NY
    Posts
    557
    Thanks
    8
    Thanked 66 Times in 66 Posts

    Default

    See if this fixes it for you:

    PHP Code:
    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> 
    - Josh

  5. #5
    Join Date
    Aug 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    @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??

  6. #6
    Join Date
    Apr 2009
    Location
    Cognac, France
    Posts
    400
    Thanks
    2
    Thanked 57 Times in 57 Posts

    Default

    Have you tried using strlen(), ie

    PHP Code:
                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
                } 

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •