Jump to content


Photo

Multiple mysql loops


  • Please log in to reply
7 replies to this topic

#1 oracle259

oracle259
  • Members
  • PipPipPip
  • Advanced Member
  • 119 posts

Posted 29 September 2006 - 03:51 AM

How can i do multiple mysql loops without having to do while ( ) more than once

#2 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 29 September 2006 - 09:15 AM

Could you explain in more detail what you're trying to do. ALsi if you want to do a loop within another loop you'll have to use either a while loop, a for loop, foreach loop

#3 oracle259

oracle259
  • Members
  • PipPipPip
  • Advanced Member
  • 119 posts

Posted 29 September 2006 - 03:42 PM

ok. I have the following code. The problem is this, say $user = "bob.camsara and $reserved names are bob and sara the output would be %%%.camsara.

my question is how can i modify the code to go through the entire result to get %%%.cam%%%%.


while ($res = @mysql_fetch_array($result)) {
 if (stristr(trim($user, $reserved)) {
   $length = strlen($reserved);
     for ($k = 1; $k  <= $length; $k++) {
      $percent = "%";
}

$newuser = eregi_replace($reserved, $percent, trim($user));
$percent = " ";

    }
 }

echo $newuser;



#4 jrodd32

jrodd32
  • Members
  • PipPipPip
  • Advanced Member
  • 41 posts

Posted 29 September 2006 - 05:05 PM

couldn't you use the strpos() function and search for your reserved words and then replace them with the % character

#5 oracle259

oracle259
  • Members
  • PipPipPip
  • Advanced Member
  • 119 posts

Posted 29 September 2006 - 05:12 PM

i'm not familiar with strpos how would i go about doing that.

#6 jrodd32

jrodd32
  • Members
  • PipPipPip
  • Advanced Member
  • 41 posts

Posted 29 September 2006 - 05:22 PM

strpos(string haystack, string needle) it will return the position of the needle within the haystack.

For example:
$string1=strpos("The cat sat on the mat", "cat");

would return 4

so once you know where in the string the data is that you don't want to display you could simply create a for loop that would print the % character you were using.

#7 oracle259

oracle259
  • Members
  • PipPipPip
  • Advanced Member
  • 119 posts

Posted 29 September 2006 - 05:29 PM

ok i read up on strpos but it still failed to account for both reserved names

#8 jrodd32

jrodd32
  • Members
  • PipPipPip
  • Advanced Member
  • 41 posts

Posted 29 September 2006 - 05:31 PM

I think that you would have to run it once for each reserved name




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users