1

i want to do a check if a user id exists in all of the table rows i search for.

edit sorry i was missleading i think. I want to check if the user has read all of the articles in a category, i have a forum front end displaying the categories and within the categories are the articles.

On the categories screen i want to display an image ALL ARTICLES READ or NOT ALL ARTICLES READ, so some how i need to loop through all of the articles per category which is what the example query below is doing and check if user id exists in ALL returned rows if yes then then all articles have been read if there are some rows missing the users id then some articles have not been read

This is my sql query

$colname_readposts = "-1";
if (isset($_GET['Thread_Category_Article_id'])) {
$colname_readposts = $_GET['Thread_Category_Article_id'];
}
mysql_select_db($database_test, $test);
$query_readposts = sprintf("SELECT Thread_Article_User_Read FROM Thread_Articles WHERE             Thread_Category_Article_id = %s", GetSQLValueString($colname_readposts, "int"));
$readposts = mysql_query($query_readposts, $cutthroats) or die(mysql_error());
$row_readposts = mysql_fetch_assoc($readposts);
$totalRows_readposts = mysql_num_rows($readposts);

How can i check if all of the rows returned contain the users id? The idea is to check to see if user has read all the articles if yes show READ if no show UNREAD.

The results per row are like so 0,15,20,37 these are id's entered when a user views a post. i have managed to get this check for a single article to show if the user has read a specific article but unsure how i would check multiple:

here is my single article check:

<?php
$userid = $_SESSION['loggedin_id'];
$userreadlist = $row_readposts['Thread_Article_User_Read'];
$myarray = (explode(',',$userreadlist));
if (in_array($userid,$myarray )){ 
?>
html image READ
<?php } else { ?>
html image UNREAD
<?php } ?>

Any help would be appreciated. Carl

0

1 Answer 1

1

First up, forget the mysql_* functions; ext/mysql is a deprecated API as of PHP 5.5 - so it's a really good idea to use mysqli or PDO.

From what I gather you're actually trying to see if any of those results contain a specific user's id? If so, do this in the SQL query:

SELECT Thread_Article_User_Read FROM Thread_Articles WHERE 
    Thread_Category_Article_id = %s AND UserID = %s

And supply this the User's ID as a second argument. (Syntax may not be 100% correct, but it should prove a point)

If you mean you want to check there there is any user's ID - then once again; do this in SQL:

SELECT Thread_Article_User_Read FROM Thread_Articles WHERE 
    Thread_Category_Article_id = %s AND UserID IS NOT NULL

This will ensure there is a valid value for 'UserID'.

Naturally replace 'UserID' with your column name if you base your solution on one of these examples.

However, if you're dumping out ALL the results from a table - and you need to see if your user's ID is present in a certain column (like you do!); then you can actually just adapt the logic that you're using on your single article page. Which should give you something like this...

$userid = $_SESSION['loggedin_id'];
$colname_readposts = "-1";
if (isset($_GET['Thread_Category_Article_id'])) {
    $colname_readposts = $_GET['Thread_Category_Article_id'];
}

/* connect to db and run query; CHANGE ME TO SOMETHING NOT DEPRECATED */
mysql_select_db($database_test, $test);
$query_readposts =
  sprintf("SELECT Thread_Article_User_Read FROM Thread_Articles WHERE
    Thread_Category_Article_id = %s", GetSQLValueString($colname_readposts, "int"));
$readposts = mysql_query($query_readposts, $cutthroats) or die(mysql_error());


/* loop through all returned items */
while($row = mysql_fetch_assoc($readposts)) {
    /* repeat the check you used on an individual
       row on the single article page. i.e: */

    $userreadlist = $row['Thread_Article_User_Read'];
    $myarray = (explode(',',$userreadlist));

    if (in_array($userid,$myarray )){
        /* user has read */
    } else {
        /* user hasn't read */
    }
}

If that code you worked for a single page then it should work in the above; as for every iteration of the loop you're working on a single row - just as you were on the single page. If the data is coming from the same table then the column names etc match up and it will work.

Or, if you just want to know if there are any unread posts at all, replace the loop with this one...

/* loop through all returned items */
$read = true;
while($row = mysql_fetch_assoc($readposts)) {
    /* repeat the check you used on an individual
       row on the single article page. i.e: */

    $userreadlist = $row['Thread_Article_User_Read'];
    $myarray = (explode(',',$userreadlist));

    if (! in_array($userid,$myarray )){
        $read = false;
        break;
    } 
}

if( $read ){
   /* all pages are read */
} else {
   /* there are unread pages */
}
Sign up to request clarification or add additional context in comments.

8 Comments

Hi Fergus, appreciate the advice tho regarding the depreciation i'm an amateur at PHP so i will look into this, i take it this will cause me problems in the future and should update my code? i have also updated my question as it wasn't worded very well
Hey Carl, No worries - I think I've posted something that will work. Essentially, mysql_ functions have been phased out slowly - and as of the latest PHP version they aren't supported. So eventually it will be an issue when the latest version of PHP is mainstream. However, transferring code to use mysqli is pretty trivial, although eventually you may want to look at PDO which is very good and has some handy features!
Also, forget what the other poster who deleted his answer was saying - an extra comma wont make a blind bit of difference to checking whether a value is in the array or not. You will just get an extra item in the array which is a blank string; "". Whatever explode() returns will be able to go through in_array() no problem. phpFiddle;
Hi Fergus, i tested it and it works but it repeats the read or unread on the amount of times the loop cut-throats.co.uk/zztest.php?Thread_Article_Head_id=12 this is my test page
this for example is read as i have read all the posts cut-throats.co.uk/zztest.php?Thread_Article_Head_id=10
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.