Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Regular Coder dniwebdesign's Avatar
    Join Date
    Dec 2003
    Location
    Carrot River, Saskatchewan
    Posts
    838
    Thanks
    15
    Thanked 9 Times in 9 Posts

    First Character Not Always?

    PHP Code:
    for($i=0;$i<$counter;$i++)
    {    
        if(
    $i!=($counter-1)) {
            
    $music explode(" - ",$theexplode[$i]);
            if(
    $music[2]=="m") {
                
    $fileType "MP3";
            }
            elseif(
    $music[2]=="s") {
                
    $fileType "SID";
            }
            elseif(
    $music[2]=="o") {
                
    $fileType "OGG";
            }
            elseif(
    $music[2]=="w") {
                
    $fileType "WAV";
            }
            
            echo 
    $i." ";
            
    $theArtist $music[0];
            echo 
    $theArtist{0}."<br>";

            
    //echo "<tr  onMouseOver=\"this.style.background='#F0F0F0'\" onMouseOut=\"this.style.background='#FFFFFF'\"><td class='leftcell'>".($i+1)."</td><td class='regcell'>".$music[0]."</td><td class='regcell'>".$music[1]."</td><td class='rightcell'>".$fileType."</td></tr>";



        
    }

    I have the above for loop which gets info from an array.
    Just ignore the last commented table row (it works fine). What I am having problems with is statement before it. It is only getting the character from the first loop. After it loops once it will not get the character for the rest of the time it's looping and I don't know why. What am I doing wrong?
    Dawson Irvine
    CEO - DNI Web Design
    http://www.dniwebdesign.com

  • #2
    Regular Coder devinemke's Avatar
    Join Date
    Dec 2004
    Location
    NYC
    Posts
    443
    Thanks
    0
    Thanked 12 Times in 11 Posts
    the curly brace syntax "{}" immediately following a string is used to return specific characters within that sting. see this example (lifted directly from the manual page on strings):
    PHP Code:
    <?php
    // Get the first character of a string
    $str 'This is a test.';
    $first $str{0};

    // Get the third character of a string
    $third $str{2};

    // Get the last character of a string.
    $str 'This is still a test.';
    $last $str{strlen($str)-1}; 

    // Modify the last character of a string
    $str 'Look at the sea';
    $str{strlen($str)-1} = 'e';         
    ?>
    anyway in your line of code here:
    PHP Code:
    echo $theArtist{0}."<br>"
    you are echoing only the first character of $theArtist.

    the broader issue here is that you seem to be overcomplicating this whole process. post an example of what you array looks like.

  • #3
    Regular Coder dniwebdesign's Avatar
    Join Date
    Dec 2003
    Location
    Carrot River, Saskatchewan
    Posts
    838
    Thanks
    15
    Thanked 9 Times in 9 Posts
    That is what I want... I want to echo the first character of $theArtist however it will only echo it on the first loop (of the for loop). After it does the loop once it doesn't echo it.
    Dawson Irvine
    CEO - DNI Web Design
    http://www.dniwebdesign.com

  • #4
    Regular Coder dniwebdesign's Avatar
    Join Date
    Dec 2003
    Location
    Carrot River, Saskatchewan
    Posts
    838
    Thanks
    15
    Thanked 9 Times in 9 Posts
    Here:
    The array consists of an artist, title and format as it goes through the array it explodes it into that array.
    So for example...
    PHP Code:
    $theArray -> {
       [
    0] = Elvis
       
    [1] = Love Me Tender
       
    [2] = m

    then the next loop it would be something like:
    PHP Code:
    $theArray -> {
      [
    0] = Elvis
      
    [1] = Heartbreak Hotel
      
    [2] = m

    However I would only get the "E" on the first loop. It will not get it for any loop after that, if I am making myself clear.
    Dawson Irvine
    CEO - DNI Web Design
    http://www.dniwebdesign.com

  • #5
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    Your code works, unless there are only two entries in the $theexplode array, due to the questionable if statment inside the for loop, which is bypassing the entire code block on the last array element.

    Instead of the if statement, modify the for loop to stop processing one iteration early, i.e.

    PHP Code:
    for($i 0$i $counter 1$i++) 
    I'd also suggest a switch statement for the $music[2] element, rather than nested if statements. It's just more better.

  • #6
    Regular Coder dniwebdesign's Avatar
    Join Date
    Dec 2003
    Location
    Carrot River, Saskatchewan
    Posts
    838
    Thanks
    15
    Thanked 9 Times in 9 Posts
    Well... depsite your suggestions it doesn't really solve the question at hand now, does it? What I'm trying to do is get the first character of the artist's name, and as you said it works. Except it doesn't, which is why I'm so confused. So here we go.

    Here is my PHP file:
    http://www.dni-server.no-ip.com/allstar/index.phps

    Current Output:
    http://www.dni-server.no-ip.com/allstar/index.php

    And I know my html is out of place and stuff, I'm just trying to get it to do what I want to do at the moment and worry about formatting later.
    Dawson Irvine
    CEO - DNI Web Design
    http://www.dniwebdesign.com

  • #7
    Regular Coder
    Join Date
    Mar 2006
    Posts
    121
    Thanks
    0
    Thanked 0 Times in 0 Posts
    OK your problem is that your artist name value has a new line character at the start. Your code is thinking that this new line character is the first character of the name so it prints out that instead of the letter.

    You'll need to remove the new line character before echoing the first character of the artists name. Using something like:
    Code:
        $theArtist = preg_replace("/[\r\n]+[\s\t]*[\r\n]+/", "\n", $music[0]);
        $theArtist = preg_replace("/^[\s\t]*[\r\n]+/", "", $theArtist);
        echo $theArtist{0}."<br>";
    Or better yet - try to remove it from the music array as it gets read in.

  • #8
    Regular Coder dniwebdesign's Avatar
    Join Date
    Dec 2003
    Location
    Carrot River, Saskatchewan
    Posts
    838
    Thanks
    15
    Thanked 9 Times in 9 Posts
    Thanks... works like a charm. ;-)
    Dawson Irvine
    CEO - DNI Web Design
    http://www.dniwebdesign.com


  •  

    Posting Permissions

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