Working on a function that prints out all database rows that end with _n where n is single digit number 1 - 9 first and then prints rows that do not end in _n. So far I have been trying to use this code

$theQuery="SELECT * FROM tableContent"
while($row=mysql_query($theQuery))
   {
      if(substr($row[0],-2,1)=="_")
         {
            //DO WHAT IT DOES FOR this_1, this_2, this_3 first
         }
   }
while($row=mysql_query($theQuery))
   {
      if(substr($row[0],-2,1)!="_")
         {
            //DO WHAT IT DOES FOR this, that, andWHatHaveYou afterwords
         }
   }

I don't know why the !="_" doesn't get done.

This is the exact code I have implemented but doesnt work 100%

$query="SELECT * FROM rt_content";
$contents=mysql_query($query);
if($contents)
	{
$baseSection = (substr($row[0],-2,-1)=="_")? substr($row[0],0,-2):$row[0];
echo "<table class=\"lined\" style=\"width:100%\"><tr><td class=\"lined\">options</td><td class=\"lined\">contentName</td><td class=\"lined\">en</td><td class=\"lined\">es</td></tr>\n";
while( $row=mysql_fetch_array($contents,MYSQL_NUM) )
		{
				if( substr($row[0],-2,1)=="_" )
						{
											echo "<tr><td class=\"lined\"><a href=\"admin.html?content=edit;".$baseSection."\">EDIT</a></td><td class=\"lined\"><a href=\"admin.html?content=view;".$baseSection."\">".$row[0]."</a></td><td class=\"lined\">".$row[1]."</td><td class=\"lined\">".$row[2]."</td></tr>\n";
									}
		}
while( $row=mysql_fetch_array($contents,MYSQL_NUM) )
		{
							if( substr($row[0],-2,1)!="_" )
						{
								echo "<tr><td class=\"lined\"><a href=\"admin.html?content=edit;".$baseSection."\">EDIT</a></td><td class=\"lined\"><a href=\"admin.html?content=view;".$baseSection."\">".$row[0]."</a></td><td class=\"lined\">".$row[1]."</td><td class=\"lined\">".$row[2]."</td></tr>\n";
						}
		}
			echo "</table>\n";
	}

Does anyone see anything wrong with this or can do it better?

Recommended Answers

All 6 Replies

Member Avatar for iamthwee

Take out the if condition and put some echos there to see what output you're getting.

I'm curious about the $baseSection = (substr($row[0],-2,-1)=="_") -1 here in line 5?

I put that on the wrong line. The array $row does not exist yet and I guess is equal to NULL and furthermore, that last minus 1 should just be one.
This will work anywhere within the while.

while( $row=mysql_fetch_array($whatever) ) //If not valid query fetch array should return null
{
$baseSection = (substr($row[0],-2,-1)=="_")? substr($row[0],0,-2):$row[0];
echo $baseSection;//This dumps first column contents.
}

I have also found another way to accomplish what I am talking about.
In this manner a new array is created, and populated with the elements for each element that does not meet the inital list. This is an example:

$theQuery="SELECT * FROM tableContent"
$singleElements=array();
   while($row=mysql_query($theQuery))
      {
         if( substr($row[0],-3,1)=="_" || !(substr($row[0],-2,1)=="_") )
           {
               array_push($singleElements,array($row[0],$row[1],$row[2]) );
           }
         if(substr($row[0],-2,1)=="_")
           {
      //DO WHAT IT DOES FOR this_1, this_2, this_3 first
           }
      }
      if($singleElements)
         {
          foreach($singleElements as $singleElement)
             {
       	         echo "";
             }
         }

This one will work with X_n first( Where X is any string and n is number 1-9 ) and then display X_nn (where nn is two digit numbers) or uniquely named elements.
What do you think?

Member Avatar for iamthwee

Have you thought about using regular expressions.

I have read through a few articles about them
But I still haven't quite gotten the hang of them.

why dont you use if else if instead of checking the substring every time ?

if(substr($row[0],-2,1)=="_") { 
//$row[0] has only 1 digit after the underscore
} else if (substr($row[0],-3,1)=="_"){
      //$row[0] has 2 digits after the underscore
} else {
   // $row[0] doesnt have an underscore
}

This will work, i am sure.

Yeah, this works. A function could be made to perform the foreach statements instead of coding. Can this be made into a function that returns an array of the order of the elements.

return array($singleElements, $singleDigits, $doubleDigits);?

then the foreach statements could be made into a function that works on an array;

if(func_num_args()==0 || func_){
   foreach($func_get_arg(0) as $elements){
      foreach($elements as $singleElement){
         echo "List ".print_r($singleElement)."<br />";
      }
   }
}

Honestly I don't know if the above code would work, but below here does.
Thanks for the tips. Let me know what you think.

$theQuery="SELECT * FROM tableContent"
$singleElements=array();
$singleDigits=array();
$doubleDigits=array();
while( $row=mysql_query($theQuery) ){
   if(substr($row[0],-2,1)=="_"){
      array_push( $singleDigits, array($row[0],$row[1],$row[2]) );
   } else if( substr($row[0],-3,1)=="_" ){
      array_push( $doubleDigits, array($row[0],$row[1],$row[2]) );
   }
   else {
      array_push( $singleElements, array($row[0],$row[1],$row[2]) );
   }
}
foreach($singleElements as $singleElement){
      echo "List ".print_r($singleElement)."<br />";
}
foreach($singleDigits as $singleDigit){
      echo "List ".print_r($singleDigit)."<br />";
}
foreach($doubleDigits as $doubleDigit){
      echo "List ".print_r($doubleDigit)."<br />";
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.