SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Oct 2011
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Need some help with this switch inside foreach loop????

    I have script which takes data from mysql and inputs into a foreach loop. I then want to replace certain strings with another one depending on what the original string is. The data in mysql output in random order. It outputs everything the way I want except when it comes to time to replace certain strings with another one. I've search other similar threads and tried many of the suggestions, but didn't get the results I'm looking for. Can somebody give me a hand with this please???


    $data=(script retrieving info from mysql);

    foreach($data['data']['data2']['data'] as $variable) {
    $new_variable = $variable['data2'];
    $new_variable = preg_replace('/\s*\s+/', '', $new_variable);

    switch($new_variable){
    case "apple":
    $z = "red";
    break;
    case "bannanas":
    $z = "yellow";
    break;
    case "grapes":
    $z="purple";
    break;
    case "oranges":
    $z="orange";
    break;
    }

    echo $z."br/>";
    }


    The script echos out $new_variable perfectly fine, but when I add the switch part, it doesn't output anything or it outputs several lines of "red".
    Thanks for the help in advance.

  2. #2
    SitePoint Addict MrBaseball34's Avatar
    Join Date
    Sep 2002
    Location
    Round Rock, Texas
    Posts
    277
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    try this:
    Code PHP:
    switch(trim($new_variable))

    It is most likely that your strings are returning not trimmed. Why are you doing this in PHP, anyway? Return the strings you want in the SQL instead. Can you post your SQL?
    MrBaseball34
    Hook'Em Horns!


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
  •