SitePoint Sponsor

User Tag List

Page 1 of 3 123 LastLast
Results 1 to 25 of 52
  1. #1
    SitePoint Addict
    Join Date
    Aug 2006
    Posts
    375
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Help with arrays

    I have the following tables :

    photos (Photo_ID as PK)
    keywords (Keyword_ID as PK)
    photokeywords (Photo_ID, Keyword_ID (ints) and Category (text)

    I have it set up so that users can use checkboxes to add keywords to each photo.

    The page I'm coming unstuck on is the page where users can edit the existing keyword selection.

    It's pretty much there with the following code :

    Code:
    mysql_select_db($database_Photolibrary, $Photolibrary); 
    $query_Keyword_Match = sprintf("SELECT * FROM photokeywords WHERE Photo_ID = %s", $Photo_ID); 
    $Keyword_Match = mysql_query($query_Keyword_Match, $Photolibrary) or die(mysql_error()); 
    $photokeywords = array(); 
    while ($row_Keyword_Match = mysql_fetch_assoc($Keyword_Match)) { 
        $photokeywords[] = $row_Keyword_Match['Keyword_ID']; 
    } 
    $totalRows_Keyword_Match = mysql_num_rows($Keyword_Match); 
    $sql = "SELECT * FROM Keywords ORDER BY Keyword_ID";
    $keywordArray = array(); 
    $query = mysql_query($sql); 
    echo('<table>');
    
    while ($result=mysql_fetch_assoc($query)) { 
        $keywordArray[$result['Category']][] = $result; 
    } 
    foreach ($keywordArray as $categoryData) { 
        echo('<tr>'); 
        foreach ($categoryData as $keywordData) { 
    		
            echo('<td><input '); 
            if (in_array($keywordData['Keyword_ID'],$photokeywords)) { 
                echo "checked "; 
            } 
            echo('name="ckbox['.$keywordData['Keyword_ID'].']" type="checkbox" class="tickbox2" id="ckbox['.$keywordData['Keyword_ID'].']"></td>'); 
            echo('<td>'.$keywordData['Keyword'].'</td>'); 
            if ($counter==3) { 
                echo('</tr><tr>'); 
                $counter = 0; 
            } else { 
                $counter++; 
            } 
        } 
        echo('</tr>'); 
    	echo($categoryData['Category']);
    }
    This shows all the keyword checkboxes correctly, but what I'm trying to do is break them up by Category, ie :

    Places of Interest

    checkbox 1 ; checkbox 2 ; checkbox 3 etc

    Flora and fauna

    checkbox 13 ; checkbox 14 ; checkbox 14 etc

    etc

    The closest I've gotten is changing :

    Code:
     
    foreach ($categoryData as $keywordData) { 
    echo('<tr>');
    echo('<td><input ');
    to

    Code:
       
    foreach ($categoryData as $keywordData) { 
    echo('<tr>'.$keywordData['Category']).'</tr>';
    echo('<td><input ');
    But what that does is list the Categories at the top before the checkboxes, rather than the structure described above.

    Any pointers on exactly how I can slot this in?

    Hope that makes sense.

    Cheers,
    Iain

  2. #2
    SitePoint Evangelist cronsrcs's Avatar
    Join Date
    Oct 2004
    Location
    UK
    Posts
    500
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I would achieve the desired output slightly differently:

    I would loop through each of the photo keywords checking the value of the Category assigned to that keyword.....If the category is different from the last keyword, then print out the heading for the Category, if not simply print out the check box.

    A cpl of things to keep in mind - when you select the keywords, order by category so that you have them in order.

    You may also want to create a lookup table of Categories instead of storing the category as text in the photokeyword tbl....
    www.silver-rocket.com
    Creative Solutions for this planet and beyond...
    www.onelifeonebus.com
    Fear fading away, not burning out.

  3. #3
    SitePoint Addict
    Join Date
    Aug 2006
    Posts
    375
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Cheers - would that be that much different code wise?

    I've basically been doing this project for work on the grounds that I've done quite a bit of web design stuff in my own time, and am just about at the level of doing database stuff, but still pretty new to PHP. So whilst I just about get the basics, when it comes to loops and arrays I'm at my limits. I got a bit of help getting this far, and this is pretty much the last bit of functionality I need to work in. (it was originally working fine without arrays, apart from persisting the existing selected keywords.)

    I've had a play around, and been reading some tutorials, and can follow the basic premise, but just not so sure on fitting in exactly what I'm trying to do here.

  4. #4
    SitePoint Evangelist cronsrcs's Avatar
    Join Date
    Oct 2004
    Location
    UK
    Posts
    500
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok,

    here is a function that I used to display links under link types.....You can adapt it to your case:

    PHP Code:
    function display_links($links_array)
    {
        
    //the current type of link that we are dealing with
        
    $current_type '';
        
    // get a counter of the number of links so that we know when to close the last </ul>
        
    $num_links count($links_array);
        
    //counter to track which link we are up to
        
    $counter 1;
        if(
    is_array($links_array))
        {
            foreach(
    $links_array as $link)
            {
                
    $link_type $link['link_type'];
                
    $link_url $link['link_url'];
                
    $link_title $link['link_title'];
                
    $link_desc $link['link_desc'];
                
                
    //if the current link type is not the same as the type of the link we are on in the foreach
                
    if($current_type <> $link_type)
                {
                    
    //close the ul
                    
    echo "</ul>\n";
                    
    //print the link type as a header
                    
    echo "<h2>$link_type</h2>\n";
                    
    //open the <ul>
                    
    echo "<ul>\n";
                    
    $current_type $link_type;
                }
                echo 
    "\t<li><a href=\"http://www.".$link_url."\" target=\"_blank\" title=\"".$link_title." - ".$link_desc."\">".$link_title."</a> - ".$link_desc."</li>\n";
                
    //check if this is the last link to display
                
    if($counter==$num_links)
                {
                    echo 
    "</ul>\n";
                }
                
    //increment the counter
                
    $counter ++;
            }
        }
        else
        {
            echo 
    "<h2>No links&hellip;</h2>\n";
            echo 
    "<p>There are no links to display.</p>\n";
        }


    You can output your output similar to that above - replace the $links_array that I use to your array...and loop through that way...You dont have to use a function like I did, just loop through (foreach()) as I have in my function.

    let me know if you need anymore help
    www.silver-rocket.com
    Creative Solutions for this planet and beyond...
    www.onelifeonebus.com
    Fear fading away, not burning out.

  5. #5
    SitePoint Evangelist cronsrcs's Avatar
    Join Date
    Oct 2004
    Location
    UK
    Posts
    500
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just to clarify - the link_type column in my example would be the Category in your example....and the link in mine is the keyword in yours
    www.silver-rocket.com
    Creative Solutions for this planet and beyond...
    www.onelifeonebus.com
    Fear fading away, not burning out.

  6. #6
    SitePoint Addict
    Join Date
    Aug 2006
    Posts
    375
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Cheers - looking at that I can follow what's going on, even if I'm far less sure about what needs to go exactly where.

    Am I right in thinking that the ultimate solution here involves part (or all) of the code I had to begin with (for outputting) plus a modified version of your code there?

    To begin with, I've renamed anything to do with links, to 'Categories' - for reference as much as anything.

    Also split it in two, at the point I'm thinking the existing code should go, ie :

    Code:
    <?php
    function display_categories($categories_array) 
    { 
        //the current type of link that we are dealing with 
        $current_type = ''; 
        // get a counter of the number of links so that we know when to close the last </ul> 
        $num_categories = count($categories_array); 
        //counter to track which link we are up to 
        $counter = 1; 
        if(is_array($categories_array)) 
        { 
            foreach($categories_array as $category) 
            { 
                $category_type = $category['Category'];
    and then as part of this for each loop, I want to be outputting the checkboxes, followed by :

    Code:
    //if the current link type is not the same as the type of the link we are on in the foreach 
                if($current_type <> $category_type) 
                { 
                    //close the ul 
                    echo "</ul>\n"; 
                    //print the link type as a header 
                    echo "<h2>$category_type</h2>\n"; 
                    //open the <ul> 
                    echo "<ul>\n"; 
                    $current_type = $category_type; 
                } 
                echo "\t<li><a href=\"http://www.".$link_url."\" target=\"_blank\" title=\"".$link_title." - ".$link_desc."\">".$link_title."</a> - ".$link_desc."</li>\n"; 
                //check if this is the last link to display 
                if($counter==$num_links) 
                { 
                    echo "</ul>\n"; 
                } 
                //increment the counter 
                $counter ++; 
            } 
        } 
    }
    Which is telling the page what to do if it's a new category?

    Or am I barking up the wrong tree?

    Actually, looking at it, some of those renamings I've done there should probably just be $photokeywords, ie the array from my original code.

    It's just a case of combining the two somehow.

  7. #7
    SitePoint Evangelist cronsrcs's Avatar
    Join Date
    Oct 2004
    Location
    UK
    Posts
    500
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes basically -

    PHP Code:
    if($current_type <> $category_type
    The above line checks if the $current_type (ie Current Category) stored is not the same as the category that the current (ie the keyword that we are processing in the loop) keyword belongs to....In my case, if they were not equal, i printed closed the <ul> from the last category, printed an <h2> and opened a new <ul>....

    In your case you will need to display the heading of the new category - perhaps closing the <tr> from the last category and opening a new row for the new category:

    ie
    PHP Code:
    echo "<tr><td>$category_name</td></tr>\n"
    And then printing out the checkboxes for the keyword.....

    Does that make sense?
    www.silver-rocket.com
    Creative Solutions for this planet and beyond...
    www.onelifeonebus.com
    Fear fading away, not burning out.

  8. #8
    SitePoint Addict
    Join Date
    Aug 2006
    Posts
    375
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It does make sense, but I'n not sure exactly how the two fit together. I've been playing around with it, putting bits where I imagine they would go, but not managed to get it right so far.

    I've redone your code as :

    Code:
    <?php
    		function display_categories($categories_array) 
    { 
        //the current type of keyword that we are dealing with 
        $current_type = ''; 
        // get a counter of the number of categories so that we know when to close the last </ul> 
        $num_links = count($categories_array); 
        //counter to track which category we are up to 
        $counter = 1; 
        if(is_array($categories_array)) 
        { 
            foreach($categories_array as $category) 
            { 
                $category_type = $category['Category']; 
                 //if the current category is not the same as the type of the category we are on in the foreach 
                if($current_type <> $category_type) 
                { 
                    //end the current row 
                    echo "</tr>\n"; 
                    //print the link type as a header 
                    echo "<h2>$category_type</h2>\n"; 
                    //start a new row <tr> 
                    echo "<tr>\n"; 
                    $current_type = $category_type; 
                } 
    	echo "\t<li><a href=\"http://www.".$link_url."\" target=\"_blank\" title=\"".$link_title." - ".$link_desc."\">".$link_title."</a> - ".$link_desc."</li>\n"; 
                //check if this is the last link to display 
                if($counter==$num_links) 
                { 
                    echo "</ul>\n"; 
                } 
                //increment the counter 
                $counter ++; 
            } 
        } 
    ?>
    and have my original code as :

    Code:
    <form name="form1" method="POST" action="photoKeywordsEdited.php">
    <input type='hidden' name='Photo_ID' value='<?php echo $Photo_ID; ?>'>
    <?php require_once('Connections/Photolibrary.php');
    error_reporting(E_ALL & E_STRICT);
    $Photo_ID = 1; 
    if (isset($_GET['Photo_ID'])) { 
      $Photo_ID = intval($_GET['Photo_ID']); 
    } 
    mysql_select_db($database_Photolibrary, $Photolibrary); 
    $query_Keyword_Match = sprintf("SELECT * FROM photokeywords WHERE Photo_ID = &#37;s", $Photo_ID); 
    $Keyword_Match = mysql_query($query_Keyword_Match, $Photolibrary) or die(mysql_error()); 
    $photokeywords = array(); 
    while ($row_Keyword_Match = mysql_fetch_assoc($Keyword_Match)) { 
        $photokeywords[] = $row_Keyword_Match['Keyword_ID']; 
    } 
    $totalRows_Keyword_Match = mysql_num_rows($Keyword_Match); 
    $sql = "SELECT * FROM Keywords ORDER BY Keyword_ID";
    $keywordArray = array(); 
    $query = mysql_query($sql); 
    echo('<table>');
    
    while ($result=mysql_fetch_assoc($query)) { 
        $keywordArray[$result['Category']][] = $result; 
    } 
    foreach ($keywordArray as $categoryData) { 
        echo('<tr>'); 
        foreach ($categoryData as $keywordData) { 
    		
            echo('<td><input '); 
            if (in_array($keywordData['Keyword_ID'],$photokeywords)) { 
                echo "checked "; 
            } 
            echo('name="ckbox['.$keywordData['Keyword_ID'].']" type="checkbox" class="tickbox2" id="ckbox['.$keywordData['Keyword_ID'].']"></td>'); 
            echo('<td>'.$keywordData['Keyword'].'</td>'); 
            if ($counter==3) { 
                echo('</tr><tr>'); 
                $counter = 0; 
            } else { 
                $counter++; 
            } 
        } 
        echo('</tr>'); 
    }
    ?>
    So I'm not sure how they fit together, and am a bit uncertain as to whether or not some of the names in your bit should be renamed as some of the names from the original bit?

    The sort of thing I've tried is :

    1. The original query part up to :

    Code:
    <?php require_once('Connections/Photolibrary.php');
    error_reporting(E_ALL & E_STRICT);
    $Photo_ID = 1; 
    if (isset($_GET['Photo_ID'])) { 
      $Photo_ID = intval($_GET['Photo_ID']); 
    } 
    mysql_select_db($database_Photolibrary, $Photolibrary); 
    $query_Keyword_Match = sprintf("SELECT * FROM photokeywords WHERE Photo_ID = %s", $Photo_ID); 
    $Keyword_Match = mysql_query($query_Keyword_Match, $Photolibrary) or die(mysql_error()); 
    $photokeywords = array(); 
    while ($row_Keyword_Match = mysql_fetch_assoc($Keyword_Match)) { 
        $photokeywords[] = $row_Keyword_Match['Keyword_ID']; 
    } 
    $totalRows_Keyword_Match = mysql_num_rows($Keyword_Match); 
    $sql = "SELECT * FROM Keywords ORDER BY Keyword_ID";
    $keywordArray = array(); 
    $query = mysql_query($sql); 
    echo('<table><tr>');
    followed by the function up to :

    Code:
    function display_categories($categories_array) 
    { 
        //the current type of keyword that we are dealing with 
        $current_type = ''; 
        // get a counter of the number of categories so that we know when to close the last </ul> 
        $num_links = count($categories_array); 
        //counter to track which category we are up to 
        $counter = 1; 
        if(is_array($categories_array)) 
        { 
            foreach($categories_array as $category) 
            { 
                $category_type = $category['Category']; 
                 //if the current category is not the same as the type of the category we are on in the foreach 
                if($current_type <> $category_type) 
                { 
                    //end the current row 
                    echo "</tr>\n"; 
                    //print the link type as a header 
                    echo "<h2>$category_type</h2>\n"; 
                    //start a new row <tr> 
                    echo "<tr>\n"; 
                    $current_type = $category_type; 
                }
    followed by the outputting :

    Code:
    foreach ($keywordArray as $categoryData) { 
        echo('<tr>'); 
        foreach ($categoryData as $keywordData) { 
    		
            echo('<td><input '); 
            if (in_array($keywordData['Keyword_ID'],$photokeywords)) { 
                echo "checked "; 
            } 
            echo('name="ckbox['.$keywordData['Keyword_ID'].']" type="checkbox" class="tickbox2" id="ckbox['.$keywordData['Keyword_ID'].']"></td>'); 
            echo('<td>'.$keywordData['Keyword'].'</td>'); 
            if ($counter==3) { 
                echo('</tr><tr>'); 
                $counter = 0; 
            } else { 
                $counter++; 
            } 
        } 
        echo('</tr>'); 
    }
                if($counter==$num_links) 
                { 
                    echo "</ul>\n"; 
                } 
                //increment the counter 
                $counter ++; 
            } 
        }

  9. #9
    SitePoint Evangelist cronsrcs's Avatar
    Join Date
    Oct 2004
    Location
    UK
    Posts
    500
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok...I think that you are close to a solution with this one.

    Looks like you just need to alter the display_galleries() function to output like you already had in your code. ie This code:

    PHP Code:
    echo('<td><input '); 
            if (
    in_array($keywordData['Keyword_ID'],$photokeywords)) { 
                echo 
    "checked "
            } 
            echo(
    'name="ckbox['.$keywordData['Keyword_ID'].']" type="checkbox" class="tickbox2" id="ckbox['.$keywordData['Keyword_ID'].']"></td>'); 
            echo(
    '<td>'.$keywordData['Keyword'].'</td>'); 
            if (
    $counter==3) { 
                echo(
    '</tr><tr>'); 
                
    $counter 0
            } else { 
                
    $counter++; 
            } 
    Which i copied from inside your foreach loop needs to be in the display_galleries() i function instead of my code...........

    It will need slight modification to echo out the line that you display for the Category heading - you should be able to see where that goes from where I was echoing out the <h2> for the link_type heading.....

    Have a go at that and post back your code and we will modify until its there
    www.silver-rocket.com
    Creative Solutions for this planet and beyond...
    www.onelifeonebus.com
    Fear fading away, not burning out.

  10. #10
    SitePoint Addict
    Join Date
    Aug 2006
    Posts
    375
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So if this was the modified version of your code :

    PHP Code:
    <?php
            
    function display_categories($categories_array

        
    //the current type of keyword that we are dealing with 
        
    $current_type ''
        
    // get a counter of the number of categories so that we know when to close the last </ul> 
        
    $num_links count($categories_array); 
        
    //counter to track which category we are up to 
        
    $counter 1
        if(
    is_array($categories_array)) 
        { 
            foreach(
    $categories_array as $category
            { 
                
    $category_type $category['Category']; 
                 
    //if the current category is not the same as the type of the category we are on in the foreach 
                
    if($current_type <> $category_type
                { 
                    
    //end the current row 
                    
    echo "</tr>\n"
                    
    //print the link type as a header 
                    
    echo "<h2>$category_type</h2>\n"
                    
    //start a new row <tr> 
                    
    echo "<tr>\n"
                    
    $current_type $category_type
                } 
        echo 
    "\t<li><a href=\"http://www.".$link_url."\" target=\"_blank\" title=\"".$link_title." - ".$link_desc."\">".$link_title."</a> - ".$link_desc."</li>\n"
                
    //check if this is the last link to display 
                
    if($counter==$num_links
                { 
                    echo 
    "</ul>\n"
                } 
                
    //increment the counter 
                
    $counter ++; 
            } 
        } 
    ?>
    The part I'm looking to modify is :

    PHP Code:
    echo "\t<li><a href=\"http://www.".$link_url."\" target=\"_blank\" title=\"".$link_title." - ".$link_desc."\">".$link_title."</a> - ".$link_desc."</li>\n"
    with

    PHP Code:
    echo('<td><input '); 
            if (
    in_array($keywordData['Keyword_ID'],$photokeywords)) { 
                echo 
    "checked "
            } 
            echo(
    'name="ckbox['.$keywordData['Keyword_ID'].']" type="checkbox" class="tickbox2" id="ckbox['.$keywordData['Keyword_ID'].']"></td>'); 
            echo(
    '<td>'.$keywordData['Keyword'].'</td>'); 
            if (
    $counter==3) { 
                echo(
    '</tr><tr>'); 
                
    $counter 0
            } else { 
                
    $counter++; 
            } 
    ?

    And I'm not sure how much from the top of my code I need :

    PHP Code:
    mysql_select_db($database_Photolibrary$Photolibrary); 
    $query_Keyword_Match sprintf("SELECT * FROM photokeywords WHERE Photo_ID = %s"$Photo_ID); 
    $Keyword_Match mysql_query($query_Keyword_Match$Photolibrary) or die(mysql_error()); 
    $photokeywords = array(); 
    while (
    $row_Keyword_Match mysql_fetch_assoc($Keyword_Match)) { 
        
    $photokeywords[] = $row_Keyword_Match['Keyword_ID']; 

    $totalRows_Keyword_Match mysql_num_rows($Keyword_Match); 
    $sql "SELECT * FROM Keywords ORDER BY Keyword_ID";
    $keywordArray = array(); 
    $query mysql_query($sql); 
    echo(
    '<table>');

    while (
    $result=mysql_fetch_assoc($query)) { 
        
    $keywordArray[$result['Category']][] = $result

    foreach (
    $keywordArray as $categoryData) { 
        echo(
    '<tr>'); 
    ie the last part from while onwards - ie is that replaced by the the function?

    I think I'm getting a bit confused as to whether I should be referencing the $keywordsArray from my original code as well as the $categories_Array in your code (as I've renamed it) - or if that should be %$keywordsArray, as already defined.

    ie is one array effectively handling everything, or is the original %keywordsArray handling the keywords and checkboxes, and the $categories_array handing the categories?

    If that makes any sense at all!

  11. #11
    SitePoint Evangelist cronsrcs's Avatar
    Join Date
    Oct 2004
    Location
    UK
    Posts
    500
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok,

    you are on the right track with replacing the output with the output from yours as in your last post.

    I was about to post to clarify about the $categories_array as you mentioned.

    Basically, the $categories_array would be best renamed to $keywords_array - it is basically an array with an entry for each row that your sql query returns - ie an entry for all keywords....I would have thought that your sql would need to select keyword_id, keyword, category....Is that ok?

    Now when you call the display_categories() function (prob also best renamed to display_keywords() !)......you obviously pass it the array of keywords ($keywords_array).......

    Now this is where your approach and mine vary slightly - i have a function (in data_functions.php) named get_keywords().....

    It look something like this (yours would be called "get_keywords()" ):

    PHP Code:
    function get_links()
    {
        
    $conn = @conn_to_db();

        
    $sql "SELECT l.link_id, l.link_url, l.link_title, l.link_desc, lt.link_type FROM link l
        INNER JOIN link_type lt ON lt.type_id = l.link_type
        ORDER BY lt.link_type ASC"
    ;

        
    $result mysql_query($sql);
        
        
    // Check that the query was successful    
        
    if (!$result)
        {
            return 
    false
        }
        
    $result db_result_to_array($result);
            
        return 
    $result;

    The piece of code to note here is the line:
    PHP Code:
    $result db_result_to_array($result); 
    It basically passes the $result array to another function that creates an array containing arrays.....

    PHP Code:
    function db_result_to_array($result)
    {
        
    $res_array = array();
        
        for (
    $count=0$row mysql_fetch_assoc($result); $count++)
        {
            
    $res_array[$count] = $row;
        }
        return 
    $res_array;

    It would pay to get your head around how that function (db_result_to_array()) works, as it is a handy way of getting query results into an array when you are selecting multiple columns and expecting multiple rows........

    Then finally in the page that is doing the display of the keywords....

    PHP Code:
    //get the keywords
    $keywords_array get_keywords;
    //output the keywords
    display_keywords($keywords_array); 
    I hope that this last part doesnt confuse you?

    If thats too much trouble, then we should be able to play around a little with the code that does the select in yours to get it to work.....
    www.silver-rocket.com
    Creative Solutions for this planet and beyond...
    www.onelifeonebus.com
    Fear fading away, not burning out.

  12. #12
    SitePoint Addict
    Join Date
    Aug 2006
    Posts
    375
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It does a bit - before I start rummaging again, is this an alternative strategy, as it seems quite a bit different?

    From the looks of it I could have simpler code doing the query?

    At the moment the data is structured in three tables, as per my first post, and the code :

    PHP Code:
    mysql_select_db($database_Photolibrary$Photolibrary); 
    $query_Keyword_Match sprintf("SELECT * FROM photokeywords WHERE Photo_ID = %s"$Photo_ID); 
    $Keyword_Match mysql_query($query_Keyword_Match$Photolibrary) or die(mysql_error()); 
    $photokeywords = array(); 
    while (
    $row_Keyword_Match mysql_fetch_assoc($Keyword_Match)) { 
        
    $photokeywords[] = $row_Keyword_Match['Keyword_ID']; 

    $totalRows_Keyword_Match mysql_num_rows($Keyword_Match); 
    $sql "SELECT * FROM Keywords ORDER BY Keyword_ID";
    $keywordArray = array(); 
    $query mysql_query($sql); 

  13. #13
    SitePoint Evangelist cronsrcs's Avatar
    Join Date
    Oct 2004
    Location
    UK
    Posts
    500
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok - thats fair enough...

    I think I neglected to consider the part about pre-selecting those that need to be checked already for a particular picture......

    You are right - you need 2 select statements
    1. A select to get the id of all of the keywords associated with the picture
    2. A select to get all of the keywords

    You need to loop through each of the keywords like we have been discussing - ie use the $current_category to display keywords under category headings....BUT then inside that looping you need to know whether the keyword that you are currently processing, is already associated with the picture - this would be done with something like in_array()....
    www.silver-rocket.com
    Creative Solutions for this planet and beyond...
    www.onelifeonebus.com
    Fear fading away, not burning out.

  14. #14
    SitePoint Evangelist cronsrcs's Avatar
    Join Date
    Oct 2004
    Location
    UK
    Posts
    500
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I just posted the above post and then realised (looking back at your original posting) that you were very close - you had the in_array() bit....

    So I think that it is just a matter of something like this in your original code:
    PHP Code:
    $current_category '';
    foreach (
    $keywordArray as $categoryData

    if(
    $current_category <> $categoryData['Category'])
                {
                    
    //open a new <tr>
                    
    echo "<tr>\n";
                    
    //print the category as a header
                    
    echo "<td>$categoryData['Category']</td>\n";
                    
    //close the <tr>
                    
    echo "</tr>\n";
                    
    $current_category $categoryData['Category'];
                }     

        echo(
    '<tr>'); 
        foreach (
    $categoryData as $keywordData) { 
            
            echo(
    '<td><input '); 
            if (
    in_array($keywordData['Keyword_ID'],$photokeywords)) { 
                echo 
    "checked "
            } 
            echo(
    'name="ckbox['.$keywordData['Keyword_ID'].']" type="checkbox" class="tickbox2" id="ckbox['.$keywordData['Keyword_ID'].']"></td>'); 
            echo(
    '<td>'.$keywordData['Keyword'].'</td>'); 
            if (
    $counter==3) { 
                echo(
    '</tr><tr>'); 
                
    $counter 0
            } else { 
                
    $counter++; 
            } 
        } 
        echo(
    '</tr>'); 

    www.silver-rocket.com
    Creative Solutions for this planet and beyond...
    www.onelifeonebus.com
    Fear fading away, not burning out.

  15. #15
    SitePoint Addict
    Join Date
    Aug 2006
    Posts
    375
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That's looking more like what I might have imagined. As you say, I think it was close, but I wasn't sure how to exactly fit it in.

    What I have at the moment is the original query code up to :

    PHP Code:
    mysql_select_db($database_Photolibrary$Photolibrary); 
    $query_Keyword_Match sprintf("SELECT * FROM photokeywords WHERE Photo_ID = %s"$Photo_ID); 
    $Keyword_Match mysql_query($query_Keyword_Match$Photolibrary) or die(mysql_error()); 
    $photokeywords = array(); 
    while (
    $row_Keyword_Match mysql_fetch_assoc($Keyword_Match)) { 
        
    $photokeywords[] = $row_Keyword_Match['Keyword_ID']; 

    $totalRows_Keyword_Match mysql_num_rows($Keyword_Match); 
    $sql "SELECT * FROM Keywords ORDER BY Keyword_ID";
    $keywordArray = array(); 
    $query mysql_query($sql); 
    echo(
    '<table>');

    while (
    $result=mysql_fetch_assoc($query)) { 
        
    $keywordArray[$result['Category']][] = $result

    Immediately followed by :

    PHP Code:
    $current_category ''
    foreach (
    $keywordArray as $categoryData

    if(
    $current_category <> $categoryData['Category']) 
                { 
                    
    //open a new <tr> 
                    
    echo "<tr>\n"
                    
    //print the category as a header 
                    
    echo "<td>$categoryData['Category']</td>\n"
                    
    //close the <tr> 
                    
    echo "</tr>\n"
                    
    $current_category $categoryData['Category']; 
                }      

        echo(
    '<tr>'); 
        foreach (
    $categoryData as $keywordData) { 
             
            echo(
    '<td><input '); 
            if (
    in_array($keywordData['Keyword_ID'],$photokeywords)) { 
                echo 
    "checked "
            } 
            echo(
    'name="ckbox['.$keywordData['Keyword_ID'].']" type="checkbox" class="tickbox2" id="ckbox['.$keywordData['Keyword_ID'].']"></td>'); 
            echo(
    '<td>'.$keywordData['Keyword'].'</td>'); 
            if (
    $counter==3) { 
                echo(
    '</tr><tr>'); 
                
    $counter 0
            } else { 
                
    $counter++; 
            } 
        } 
        echo(
    '</tr>'); 

    This is throwing up the error :

    Parse error: parse error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in C:\wamp\www\Photolibrary\editPhotoKeywords.php on line 156

    Where line 156 is the print category as header line :

    PHP Code:
    echo "<td>$categoryData['Category']</td>\n"
    I'll have a rummage here too, but unless things are quite obvious I'm not familiar enough with the syntax to spot errors.

  16. #16
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,806
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    havent been throught the entire posts but,
    PHP Code:
    echo "<td>"$categoryData['Category']."</td>\n"
    should do it
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  17. #17
    SitePoint Addict
    Join Date
    Aug 2006
    Posts
    375
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Cheers - that's fixed the error, but those Category headings still aren't appearing unfortunately. I've checked dumb stuff like making sure it is Category and not category, but that side of things seems fine.

  18. #18
    SitePoint Evangelist cronsrcs's Avatar
    Join Date
    Oct 2004
    Location
    UK
    Posts
    500
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thx spikeZ - I spotted that after I posted yesterday, but didnt get a chance to fix it
    www.silver-rocket.com
    Creative Solutions for this planet and beyond...
    www.onelifeonebus.com
    Fear fading away, not burning out.

  19. #19
    SitePoint Evangelist cronsrcs's Avatar
    Join Date
    Oct 2004
    Location
    UK
    Posts
    500
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am not sure that you need this bit of code:
    PHP Code:
     foreach ($categoryData as $keywordData) {
             
            echo(
    '<td><input ');
            if (
    in_array($keywordData['Keyword_ID'],$photokeywords)) {
                echo 
    "checked ";
            }
            echo(
    'name="ckbox['.$keywordData['Keyword_ID'].']" type="checkbox" class="tickbox2" id="ckbox['.$keywordData['Keyword_ID'].']"></td>');
            echo(
    '<td>'.$keywordData['Keyword'].'</td>');
            if (
    $counter==3) {
                echo(
    '</tr><tr>');
                
    $counter 0;
            } else {
                
    $counter++;
            }
        } 
    Basically in the foreach loop about 10 lines above it
    PHP Code:
    foreach ($keywordArray as $categoryData)

    you are already looping through the array of keywords.....So basically when looping through each of the keywords, we are performing a number of tasks:
    1. Check if the category of the keyword is the same as the last keyword - if it isnt, then echo a heading for the category
    2. Echo the checkbox for the keyword, if the particular keyword_id is in the array of keywords for this photo, then we "check" the checkbox....

    Can you do a print_r($categoryData) to see what is in the array?
    Also handy to do a print_r($photokeywords) to ensure that array has the values that you are expecting to...........

    Post that result back and we will go from there...
    www.silver-rocket.com
    Creative Solutions for this planet and beyond...
    www.onelifeonebus.com
    Fear fading away, not burning out.

  20. #20
    SitePoint Evangelist cronsrcs's Avatar
    Join Date
    Oct 2004
    Location
    UK
    Posts
    500
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just to clarify - you need this code:

    PHP Code:
    foreach ($categoryData as $keywordData) {
             
            echo(
    '<td><input ');
            if (
    in_array($keywordData['Keyword_ID'],$photokeywords)) {
                echo 
    "checked ";
            }
            echo(
    'name="ckbox['.$keywordData['Keyword_ID'].']" type="checkbox" class="tickbox2" id="ckbox['.$keywordData['Keyword_ID'].']"></td>');
            echo(
    '<td>'.$keywordData['Keyword'].'</td>');
            if (
    $counter==3) {
                echo(
    '</tr><tr>');
                
    $counter 0;
            } else {
                
    $counter++;
            }
        } 
    BUT i dont think that you need to have it inside a loop;
    PHP Code:
    foreach ($categoryData as $keywordData
    www.silver-rocket.com
    Creative Solutions for this planet and beyond...
    www.onelifeonebus.com
    Fear fading away, not burning out.

  21. #21
    SitePoint Addict
    Join Date
    Aug 2006
    Posts
    375
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK - I took out that bit of code, which threw me, as I though that was the part that was displaying all the checkboxes and associated keywords on the page?

    Sure enough, when I took it out, they weren't displaying. So if that code isn't needed, which bit should be displaying the checkboxes and keywords?

    With it as it was, I did those prints.

    print_r($categoryData) seems to be showing the same for any photo I select :

    Array ( [0] => Array ( [Keyword_ID] => 93 [Keyword] => Autumn [Category] => Weather and seasons ) [1] => Array ( [Keyword_ID] => 94 [Keyword] => Bore [Category] => Weather and seasons ) [2] => Array ( [Keyword_ID] => 95 [Keyword] => Night [Category] => Weather and seasons ) [3] => Array ( [Keyword_ID] => 96 [Keyword] => Rain [Category] => Weather and seasons ) [4] => Array ( [Keyword_ID] => 97 [Keyword] => Sky / cloud [Category] => Weather and seasons ) [5] => Array ( [Keyword_ID] => 98 [Keyword] => Snow / ice [Category] => Weather and seasons ) [6] => Array ( [Keyword_ID] => 99 [Keyword] => Spring [Category] => Weather and seasons ) [7] => Array ( [Keyword_ID] => 100 [Keyword] => Summer [Category] => Weather and seasons ) [8] => Array ( [Keyword_ID] => 101 [Keyword] => Sunrise / sunset [Category] => Weather and seasons ) [9] => Array ( [Keyword_ID] => 102 [Keyword] => Winter [Category] => Weather and seasons ) )

    So that appears a bit random.

    print_r($photokeywords) looks more like it tho' - for example :

    Array ( [0] => 2 [1] => 3 [2] => 75 [3] => 76 [4] => 100 )

    Where 2, 3, 75, 76, 100 are the correct Keyword_IDs for that photo.

  22. #22
    SitePoint Addict
    Join Date
    Aug 2006
    Posts
    375
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by cronsrcs View Post
    Just to clarify - you need this code:

    PHP Code:
    foreach ($categoryData as $keywordData) {
             
            echo(
    '<td><input ');
            if (
    in_array($keywordData['Keyword_ID'],$photokeywords)) {
                echo 
    "checked ";
            }
            echo(
    'name="ckbox['.$keywordData['Keyword_ID'].']" type="checkbox" class="tickbox2" id="ckbox['.$keywordData['Keyword_ID'].']"></td>');
            echo(
    '<td>'.$keywordData['Keyword'].'</td>');
            if (
    $counter==3) {
                echo(
    '</tr><tr>');
                
    $counter 0;
            } else {
                
    $counter++;
            }
        } 
    BUT i dont think that you need to have it inside a loop;
    PHP Code:
    foreach ($categoryData as $keywordData
    OK - missed you there. With it as :

    PHP Code:
    mysql_select_db($database_Photolibrary$Photolibrary); 
    $query_Keyword_Match sprintf("SELECT * FROM photokeywords WHERE Photo_ID = %s"$Photo_ID); 
    $Keyword_Match mysql_query($query_Keyword_Match$Photolibrary) or die(mysql_error()); 
    $photokeywords = array(); 
    while (
    $row_Keyword_Match mysql_fetch_assoc($Keyword_Match)) { 
        
    $photokeywords[] = $row_Keyword_Match['Keyword_ID']; 

    $totalRows_Keyword_Match mysql_num_rows($Keyword_Match); 
    $sql "SELECT * FROM Keywords ORDER BY Keyword_ID";
    $keywordArray = array(); 
    $query mysql_query($sql); 
    echo(
    '<table>');

    while (
    $result=mysql_fetch_assoc($query)) { 
        
    $keywordArray[$result['Category']][] = $result

    $current_category ''
    foreach (
    $keywordArray as $categoryData

    if(
    $current_category <> $categoryData['Category']) 
                { 
                    
    //open a new <tr> 
                    
    echo "<tr>\n"
                    
    //print the category as a header 
                    
    echo "<td>"$categoryData['Category']."</td>\n"
                    
    //close the <tr> 
                    
    echo "</tr>\n"
                    
    $current_category $categoryData['Category']; 
                }       

        echo(
    '<tr>'); 
       
              
            echo(
    '<td><input '); 
            if (
    in_array($keywordData['Keyword_ID'],$photokeywords)) { 
                echo 
    "checked "
            } 
            echo(
    'name="ckbox['.$keywordData['Keyword_ID'].']" type="checkbox" class="tickbox2" id="ckbox['.$keywordData['Keyword_ID'].']"></td>'); 
            echo(
    '<td>'.$keywordData['Keyword'].'</td>'); 
            if (
    $counter==3) { 
                echo(
    '</tr><tr>'); 
                
    $counter 0
            } else { 
                
    $counter++; 
            } 
        
        echo(
    '</tr>'); 

    print_r($categoryData);
    print_r($photokeywords); 
    It's looking a bit ropey, as all that seems to want to do is display seven checkboxes on a row each, with no keywords.

    With that line in, all the checkboxes and keywords are present and correct, with the correct ones checked - just without the Category headings.

  23. #23
    SitePoint Evangelist cronsrcs's Avatar
    Join Date
    Oct 2004
    Location
    UK
    Posts
    500
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    mysql_select_db($database_Photolibrary$Photolibrary);
    $query_Keyword_Match sprintf("SELECT * FROM photokeywords WHERE Photo_ID = %s"$Photo_ID);
    $Keyword_Match mysql_query($query_Keyword_Match$Photolibrary) or die(mysql_error());
    $photokeywords = array();
    while (
    $row_Keyword_Match mysql_fetch_assoc($Keyword_Match)) {
        
    $photokeywords[] = $row_Keyword_Match['Keyword_ID'];
    }
    $totalRows_Keyword_Match mysql_num_rows($Keyword_Match);
    $sql "SELECT * FROM Keywords ORDER BY Keyword_ID";
    $keywordArray = array();
    $query mysql_query($sql);
    echo(
    '<table>');

    while (
    $result=mysql_fetch_assoc($query)) {
        
    $keywordArray[$result['Category']][] = $result;
    }
    $current_category '';
    foreach (
    $keywordArray as $categoryData)
    {
    if(
    $current_category <> $categoryData['Category'])
                {
                    
    //open a new <tr>
                    
    echo "<tr>\n";
                    
    //print the category as a header
                    
    echo "<td>"$categoryData['Category']."</td>\n";
                    
    //close the <tr>
                    
    echo "</tr>\n";
                    
    $current_category $categoryData['Category'];
                }       

        echo(
    '<tr>');
       
              
            echo(
    '<td><input ');
            if (
    in_array($keywordData['Keyword_ID'],$photokeywords)) {
                echo 
    "checked ";
            }
            echo(
    'name="ckbox['.$keywordData['Keyword_ID'].']" type="checkbox" class="tickbox2" id="ckbox['.$keywordData['Keyword_ID'].']"></td>');
            echo(
    '<td>'.$keywordData['Keyword'].'</td>');
            if (
    $counter==3) {
                echo(
    '</tr><tr>');
                
    $counter 0;
            } else {
                
    $counter++;
            }
        
        echo(
    '</tr>');
    }
    print_r($categoryData);
    print_r($photokeywords); 
    So with the above code you are getting all of the keywords and checkboxes, but just no category headings?

    Can you do some debugging echo's such as after this line:
    PHP Code:
    foreach ($keywordArray as $categoryData)

    try
    PHP Code:
    echo "Current category is: " .$current_category."<br";
    echo 
    "Current keywords category is: " .$categoryData['Category']."<br"
    Also, what did the print_r show you?
    It seems that there is just a small glitch with the $categoryData['Category'] being echoes etc....I just want to see whether it is getting a var or not...
    www.silver-rocket.com
    Creative Solutions for this planet and beyond...
    www.onelifeonebus.com
    Fear fading away, not burning out.

  24. #24
    SitePoint Evangelist cronsrcs's Avatar
    Join Date
    Oct 2004
    Location
    UK
    Posts
    500
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ah...just a thought

    Try
    PHP Code:
    $categoryData['Category'][2
    or
    PHP Code:
    $categoryData[2]['Category'
    instead of
    PHP Code:
    $categoryData['Category'
    when attempting to refer to the category of the keyword.

    In your select statement when you select * from keywords....which column is the category? The 3rd one?
    www.silver-rocket.com
    Creative Solutions for this planet and beyond...
    www.onelifeonebus.com
    Fear fading away, not burning out.

  25. #25
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,806
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by cronsrcs View Post
    Thx spikeZ - I spotted that after I posted yesterday, but didnt get a chance to fix it
    No worries, didn't know if you were around so jumped in - hope you dont mind
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....


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
  •