SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member
    Join Date
    Nov 2011
    Location
    The Colony, TX
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    implode array not functioning

    As part of a project I'm building, I allow my users to "tag" their links with keywords. I take the input as a comma separated string from a text input, split the tags at the comments, trim the whitespace
    and insert them into a tag table, which has an auto-incremented tagid #, and then placing the auto-incremented tagid back in the links table, (hopefully comma-seprated).
    everything up to generating that comma separated string of tag id#s is working, my tags are being put in the table correctly, and I'm not getting duplicates, (which is what this whole mess is supposed to prevent)
    What follows is my code that's attempting to take those "tags" and get the "tagid" number associated with it, place it in an array, and then generate a comma delineated string that's placed into the user_content table.

    The error I'm getting is
    Code:
    Warning: implode() [function.implode]: Invalid arguments passed in /var/www/public_html/testsite/submitlinks.php on line 47
    Code:
    foreach ($tagparts as $querytag) {
    					trim($querytag);
    					$query2 = "SELECT tagid from $uTagTable where tag='$querytag'";
    					$result2 = mysql_query($query2);
    					while ($row2 = mysql_fetch_array($result2)){
    					$tagid = $row2['tagid'];
    					echo "<p class=\"error\">tagid: $tagid</p>";
    //--> this is line 47	        implode(",",$tagid);
    					mysql_query("INSERT INTO user_links (tags) VALUES ('$tagid') WHERE url = '$link3'");
    					}
    				}
    I should point out that I've added the echo statement, to make sure the correct data is being output from the $tagid = $row2['tagid'] and it is, I get a series of paragraphs with the correct tagid#s in them.
    And, just for clarification, I'm not commenting out line 47 in my production code, just pointing out where my error is being "encountered"

  2. #2
    SitePoint Member
    Join Date
    Nov 2011
    Location
    The Colony, TX
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    a little more progress

    As usually, happens, once I post, I do a little more digging, and I think I'm making progress. I realized part of my error was that I'm not actually creating an array with the first code I posted, so I modified it as follows.
    Code:
    		foreach ($tagparts as $querytag) {
    					trim($querytag);
    					$query2 = "SELECT tagid from $uTagTable where tag='$querytag'";
    					$result2 = mysql_query($query2);
    					$tagarray = array();
    					while ($row2 = mysql_fetch_array($result2)){
    					$tagid = $row2['tagid'];
    					$tagarray[] = $tagid;
    					foreach ($tagarray as $tagid2){
    					echo "<p class=\"error\">tagid: $tagid</p>";
    					echo "<p class=\"error\">tagid2: $tagid2</p>";
    					}
    					$tagstring=(implode(",",$tagid2));
    					echo "<p class=\"error\">tagstring: $tagstring</p>";					
    					mysql_query("INSERT INTO user_links (tags) VALUES ('$tagstring') WHERE url = '$link3'");
    					}
    As before, I'm still getting a complaint about my implode call, and obviously, there aren't any tagid#s being inserted into my user_links table.

  3. #3
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Have I understood your intent?

    A user sends you tags in a string say, and a link.

    $tags = "chocolate, sundae";

    $link = "superdesserts.com";

    If your database is set up like this:

    Code:
    links table
    ===
    id | link
    =====
    23 | boringdesserts.com
    
    tags table
    ===
    id | tag
    =====
    101 | strawberry
    102 | sundae
    
    link_tag table
    =====
    link_ref | tag_ref
    ===========
    23 | 101
    23 | 102
    and after the submission I outlined above, do you expect to see this in your database?

    Code:
    links table
    ===
    id | link
    =====
    23 | boringdesserts.com
    24 | superdesserts.com  <- inserted a new link
    
    tags table
    ===
    id | tag
    =====
    101 | strawberry
    102 | sundae
    103 | chocolate <- inserted a new tag
    
    link_tag table
    =====
    link_ref | tag_ref
    ===========
    23 | 101
    23 | 102
    24 | 102 <-recognised an existing tag and entered it, with new link_ref
    24 | 103 <-added the new tags id and the the new link_ref
    Is that where you are coming from?

  4. #4
    SitePoint Member
    Join Date
    Nov 2011
    Location
    The Colony, TX
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here is how my table is currently set up:
    Code:
    links table
    ===
    id | link | tags
    =====
    23 | boringdesserts.com |
    
    tags table
    ===
    id | tag
    =====
    101 | strawberry
    102 | sundae
    and, after the submission, what I'd like to see is this
    Code:
    links table
    ===
    id | link | tags
    =====
    23 | boringdesserts.com | 101,103
    
    tags table
    ===
    id | tag
    =====
    101 | strawberry
    102 | sundae
    103 | nuts
    In this case, the user would have put "strawberry, nuts" as the tags relating to their link........

  5. #5
    SitePoint Member
    Join Date
    Nov 2011
    Location
    The Colony, TX
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    For the record, my code has grown since my original post, I can't actually tell if I'm making any headway, but it looks like this:
    Code:
    $tagparts=explode(',', $tags);
    		foreach($tagparts as $inserttag) {
    			trim($inserttag);
                           // here, we split the tag string into its individual parts and remove whitespace
    			$query1 = "SELECT tagid FROM $uTagTable WHERE tag=$inserttag";
    			$result1 = mysql_query($query1);
    			if (!$result1) {
                           // if there isn't already a tag by that exact name, we need to create a new record
    				mysql_query("INSERT INTO $uTagTable (tag) values ('$inserttag')");
    				}
    				$tagidquery = "SELECT tagid FROM $uTagTable WHERE tag = '$inserttag'";
                         // trying to generate an array of tagid#s from the tags we split up earlier
    				$tagidresult = mysql_query($tagidquery);
    				$tagrow = mysql_fetch_array($tagidresult);
    				echo "<p class=\"error\">TagRow: $tagrow";
    				$tagcolumn = array();
    				echo "<p class=\"error\">TagColumn: $tagcolumn";
    				while($tagrow = mysql_fetch_array($tagidresult)){
        			$tagcolumn[] = $tagrow[$key];
        			echo "<p class=\"error\">TagRow: $tagrow";
        			echo "<p class=\"error\">TagKey: $key";
    				}
    				echo "<p class=\"error\">FinalTagColumn: $tagcolumn";
    				$tagstring = implode(",", $tagcolumn);
    				echo "<p class=\"error\">TagString: $tagstring";
    				}
    				}
    				mysql_query("INSERT INTO user_links (url, urldesc, tags, subdate, urlprivate, userid) VALUES ('$link3', '$desc3', '$tagstring', '$subdate', '$private', '$userid')");
    the echo statements are there to help with my debugging process, and I've included the "leading up to here" code as well.
    Whats working:
    Tags are being inserted into the user_tags table, urls are being inserted into the user_links table, but, no tag id#s are being inserted into the user_links table.
    Last edited by m.andrew.dennis; Nov 28, 2011 at 11:42. Reason: clarification of my code

  6. #6
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    PHP Code:
    mysql_query("INSERT INTO user_links (url, urldesc, tags, subdate, urlprivate, userid) VALUES ('$link3', '$desc3', '$tagstring', '$subdate', '$private', '$userid')"); 
    You say that query is not doing what you expect it to do.

    So, is your PHP not assembling the string correctly, or is your mysql query badly formed?

    Change that line slightly by adding another variable which contains your sql query.
    PHP Code:

    $tag_insert 
    "INSERT INTO user_links (url, urldesc, tags, subdate, urlprivate, userid) VALUES ('$link3', '$desc3', '$tagstring', '$subdate', '$private', '$userid')";
    mysql_query($tag_insert); 
    What that means is that you can then optionally do:
    PHP Code:
    // temp line of debug
    echo $tag_insert
    onto the page, copy that output and then paste it directly into your database -- that should then give you a clue in which direction to look for the cause of the problem.

    The other way would be to go look at the last lines in your mysql log file.


Tags for this Thread

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
  •