prob with strpos

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
Little Spy
Forum Commoner
Posts: 31
Joined: Thu Oct 10, 2002 8:18 pm
Contact:

prob with strpos

Post by Little Spy »

i have this bit of code

Code: Select all

while ($data = fetcho_query($query)) {
      // Break out if it begins with split letter
      if (strpos($bchar, $dataї'name']) == 0) break;
      // Make overall rating
      $rating = round(($dataї'rating_animation'] + $dataї'rating_characters'] + $dataї'rating_music'] + $dataї'rating_plot'] + $dataї'rating_sound'] + $dataї'rating_voiceacting']) / 6, 1);
      $date = date("n/j/Y", $dataї'revid']);
      $search = array('<LISTBIT: Name>', '<LISTBIT: Genre>', '<LISTBIT: Type>', '<LISTBIT: Date>', '<LISTBIT: Rating>', '<LISTBIT: ID>');
      $replace = array($data&#1111;'name'], $data&#1111;'genre'], $data&#1111;'type'], $date, $rating, $data&#1111;'revid']);
      $list_reviews .= str_replace($search, $replace, stripslashes($keybase&#1111;'review_listbit']));
   &#125;
the bit giving me problems is

Code: Select all

if (strpos($bchar, $data&#1111;'name']) == 0) break;
i want to break out and not display output if the first letter of the variable starts with a certain letter, its like a filter out and only show names with a-g on the page $bchar is passed by the url so if you had bchar=b it would filter out all entries that come after b, right now it is just completely stoping all entries from showing regardless of what $bchar is equal to
User avatar
Stoker
Forum Regular
Posts: 782
Joined: Thu Jan 23, 2003 9:45 pm
Location: SWNY
Contact:

Post by Stoker »

From the manual:

int strrpos ( string haystack, char needle)
Returns the numeric position of the last[/red] occurrence of needle in the haystack string.

I would use substr and string comparison instead, something like

$mychar = 'A';
$searchstring = 'A little lamb';

if (!strcmp(substr($searchstring,0,1),$mychar)) {
# the first letter of searchstring was mychar
}
User avatar
Stoker
Forum Regular
Posts: 782
Joined: Thu Jan 23, 2003 9:45 pm
Location: SWNY
Contact:

Post by Stoker »

oops my bad, strpos, not strrpos...

but still,
int strpos ( string haystack, string needle [, int offset])
seems like you switches haystack and needle?

You may also want to use triple equal signs === to make sure the result is zero and integer and not a false
Little Spy
Forum Commoner
Posts: 31
Joined: Thu Oct 10, 2002 8:18 pm
Contact:

Post by Little Spy »

alright with your help I got it to work with this argument

Code: Select all

if (substr($data&#1111;'name'],0,1) == $split) break;
but i have been interduced to a new simple prob i didnt want to start a whole new thread for

let's say $data['name'] is equal to "Choose This"

and $split is equal to c

how can i make it so "C can be equal to c" or possibly convert $split to the opposite case and do and OR in the if statement
User avatar
Stoker
Forum Regular
Posts: 782
Joined: Thu Jan 23, 2003 9:45 pm
Location: SWNY
Contact:

Post by Stoker »

use strtoupper() or strtolower() on one or both...

Code: Select all

<?php
  if (!strcmp(strtoupper(substr($data['name'],0,1)),strtoupper($split))) break;
?>
Post Reply