jQuery and PHP - PHP Logic Issue

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Deuce
Forum Newbie
Posts: 12
Joined: Sun Jan 25, 2009 5:23 pm

jQuery and PHP - PHP Logic Issue

Post by Deuce »

Preface: Okay, I am pretty sure this is a PHP issue, and not jQuery, but I don't want to cross post, so if I am wrong maybe a mod can move it.

I have this PHP

Code: Select all

$catList = mysql_query("SELECT * FROM adminCategory ORDER BY CategoryName");
 
$catLinks = array();
while ($catlistRow = mysql_fetch_array($catList)) {
   $catLinks[] = '<a href="javascript&#058;;" class="sortCat" id="sortCat_'.$catlistRow['id'].'" onclick="sortItemCat('.$catlistRow['id'].')">'. $catlistRow['CategoryName'] .'</a>';
}
 
echo "<p id=\"catSortList\">" . implode(' | ', $catLinks) . "</p>";
 
 
 
$result = mysql_query('SELECT * FROM adminItems ORDER BY id' . $catSortItem);
while ($row = mysql_fetch_array($result)) {
    $textmsg = $line = str_replace("|", "</li><li>", $row['textmsg']);
    $catID = $row['category_id'];
    $catsql = "SELECT * FROM adminCategory WHERE id = " . $catID;
    $catName = mysql_query($catsql);
    while ($catRow = mysql_fetch_array($catName)) {
        print '<div id="Item_' . $row['id'] . '" class="itemListBox">
                    <img src="../content/img/items/imgresize.php?src=../content/'.$row['image_url'].'&w=100" alt="'.$row['name'].'" title="'.$row['name'].'" class="itemListImg" />
                    <div><h3 class="itemTitle">' . $catRow['CategoryName'] . ' &raquo; '.$row['name'].'</h3>
                    <p class="itemDesc"><strong>Description: </strong>'.$row['description'].'</p>
                    <p class="itemMsg"><strong>Text Message(s): </strong></p>
                    <ol class="itemMsgList"><li>'.$textmsg."</li></ol></div>
                </div>\n\n";
    }
}
This works perfectly, so Basically, it is creating a a list of all the categories as well as all the items.
The categories are linked to the jQuery so that I can show only the clicked on category.

I added this to the top of the PHP

Code: Select all

if(!empty($_POST) && !empty($_POST['catSortID'])) {
    $catSortID = $_POST['catSortID'];
    $catSortItem = " WHERE category_id = " . $catSortID;
} else {
    $catSortItem = "";
}
And for the jQuery I used

Code: Select all

function sortItemCat(id) {
    var catList = $("#sortCat_"+id).text();
    $.post('lib/loadItem.php', { catSortID: id }, function(data) {
        if(data != 'false'){
            $("#itemList").load("lib/loadItem.php");
        }else{alert(data+" -- "+id+" -- "+catList);}
    });
}
the var catList and the else alert are just for troubleshooting right now, they are unneeded and will not be in the final code.
I am not getting the else alert, so it is got into the loadItem.php code from above, but it is not actually rerunning the PHP with the sorting.

Any help or any further information would be helpful. If you need me to explain what I am doing further in depth please let me know.
Thanks! :banghead:
Deuce
Forum Newbie
Posts: 12
Joined: Sun Jan 25, 2009 5:23 pm

Re: jQuery and PHP - PHP Logic Issue

Post by Deuce »

Okay, I found some syntax errors in the PHP.

I am thinking it is the jQuery now that the PHP is functional....

Code: Select all

if(!empty($_POST) || $catSortID != "") {
    $catSortID = $_POST['catSortID'];
    $catSortItem = " WHERE category_id = " . $catSortID;
    echo '<script type="text/javascript">alert("++'.$catSortID.'++");</script>';
} else {
    $catSortItem = "";
}
 
$catList = mysql_query("SELECT * FROM adminCategory ORDER BY CategoryName");
 
$catLinks = array();
while ($catlistRow = mysql_fetch_array($catList)) {
   $catLinks[] = '<a href="javascript&#058;;" class="sortCat" id="sortCat_'.$catlistRow['id'].'" onclick="sortItemCat('.$catlistRow['id'].')">'. $catlistRow['CategoryName'] .'</a>';
}
 
echo "<p id=\"catSortList\">" . implode(' | ', $catLinks) . "</p>\n\n";
 
 
 
$result = mysql_query('SELECT * FROM adminItems'.$catSortItem.' ORDER BY id');
while ($row = mysql_fetch_array($result)) {
    $textmsg = $line = str_replace("|", "</li><li>", $row['textmsg']);
    $catID = $row['category_id'];
    $catsql = "SELECT * FROM adminCategory WHERE id = " . $catID;
    $catName = mysql_query($catsql);
    while ($catRow = mysql_fetch_array($catName)) {
        print '<div id="Item_' . $row['id'] . '" class="itemListBox">
                    <img src="../content/img/items/imgresize.php?src=../content/'.$row['image_url'].'&w=100" alt="'.$row['name'].'" title="'.$row['name'].'" class="itemListImg" />
                    <div><h3 class="itemTitle">' . $catRow['CategoryName'] . ' &raquo; '.$row['name'].'</h3>
                    <p class="itemDesc"><strong>Description: </strong>'.$row['description'].'</p>
                    <p class="itemMsg"><strong>Text Message(s): </strong></p>
                    <ol class="itemMsgList"><li>'.$textmsg."</li></ol></div>
                </div>\n\n";
    }
}
Post Reply