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 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Jan 2007
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Help needed with foreach/arrays for tagging

    I need some help with foreach/arrays - I want to add a small tagging function to a news script, but so far it's not working.

    I either get the error "Warning: Invalid argument supplied for foreach() on line 54"
    or it enters the data as "charts, tags, testing" into the database in only one row, instead of creating a new row for each of those tags.

    I have defined a $tags = array(); at the top if my file, and I have a simple text input field for typing the tags.

    This is the part of the script which posts the tags:
    PHP Code:
    $tags $_POST['tags'];
                    foreach(
    $tags as $tag) {
                        
    $tag strtolower($tag);
                        
    $tag strip_tags($tag);
                        
    $tag str_replace('ä'"ae"$tag);
                        
    $tag str_replace('ö'"oe"$tag);
                        
    $tag str_replace('ü'"ue"$tag);
                        
    $tag str_replace('ß'"ss"$tag);
                        
    $tag mysql_real_escape_string($tag);
                        
    $query_tag "INSERT INTO news_tags VALUES ('','$newsID','$tag')";
                        
    $result_tag mysql_query($query_tag);
                        if (
    $result_tag != false) {
                                echo 
    "<br />Tag '".$tag."' hinzugefügt.";
                        }
                    } 
    Can anyone help out and explain how I get a new entry for each comma seperated tag in my input field?

  • #2
    New to the CF scene
    Join Date
    Jul 2004
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi if you have defined $tags = array(); at the top of the file it will get over re-set at the point where you set it to the posted var (at this point: $tags = $_POST['tags'].

    You are getting "Warning: Invalid argument supplied for foreach() on line 54" because if the post'ed tags value is empty it will not be an array.

    You can stop this one of 2 ways:

    first is the quickest to code but can be confusing for some is using the Ternary Operator (http://php.net/operators.comparison)

    replace the line

    PHP Code:
    $tags $_POST['tags']; 
    with:

    PHP Code:
    $tags = (is_array($_POST['tags']) ? $_POST['tags'] : array()); 
    second slightly longer but more readable:

    PHP Code:
    $tags $_POST['tags'];
    if(!
    is_array($_POST['tags']))
    {
        
    $tags = array();
    }
     
    // your code 
    Hope this helps.
    Last edited by stewis; 01-30-2007 at 01:50 PM. Reason: meh give the right info eh?


  •  

    Posting Permissions

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