Page 1 of 1
Code Help please
Posted: Wed Sep 27, 2006 12:11 am
by affihq
I have ran into a bit of a problem trying to code something
Code: Select all
$data = mysql_query("SELECT * FROM readit") or die(mysql_error());
while($row = mysql_fetch_array( $data ))
{
if ($user==$row['user'])
{
$a=$a+1;
$story[$a]=$row['story'];
}
}
if ($row['id']!=$story[2] AND $row['id']!=$story[3] .... AND $row['id']!=$story[x])
Basically what I am looking for the code to do is to make an array (a quite possibly huge array, and then an if statement checking a variable (not listed) if it matches it in each instance of the array.
Just wondering if there is any other way to do this other than listing out and checking every portion of the array in one huge if statement.
Re: Code Help please
Posted: Wed Sep 27, 2006 12:23 am
by Benjamin
This should put you on the right track.
Code: Select all
$story = array();
$data = mysql_query("SELECT * FROM readit") or die(mysql_error());
while($x = mysql_fetch_array($data))
{
if ($user == $x['user'])
{
$story[] = $x['story'];
}
}
foreach ($story as $value)
{
if ($blah == $value)
{
// do something
}
}
Posted: Wed Sep 27, 2006 12:29 am
by affihq
Thanks for the help!!

Posted: Wed Sep 27, 2006 3:23 am
by affihq
didn't work out to well, tried a few different ways to make it work.
Anyways basically in Pseudo:
Basically what I want it to do is to load up an array of integers, let's say (2, 5, 6, 9)
then I want to scan the entire table
if story==array(2) and story==array(5) and story==array(6) story==array(9)
then do nothing
else
do something
by using foreach it won't let me check each variable at the same time, any solution to this?
Posted: Wed Sep 27, 2006 3:40 am
by volka
Now I understand it even less.
affihq wrote:I want it to do is to load up an array of integers, let's say (2, 5, 6, 9)
from where, how, conditions?
affihq wrote:then I want to scan the entire table
The same table as before? why?
affihq wrote:if story==array(2) and story==array(5) and story==array(6) story==array(9)
array are the previoulsy read values?
story is the value of the current row of the (re-)scan of the same table?
Please explain the purpose of the code; what are you trying to achieve, step by step?
Posted: Wed Sep 27, 2006 3:51 am
by affihq
Here is the code:
Code: Select all
$data = mysql_query("SELECT * FROM readit") or die(mysql_error());
while($x = mysql_fetch_array( $data ))
{
if ($user==$row['user'])
{
$a=$a+1;
$story[$a]=$row['story'];
}
}
//new test script end
$data = mysql_query("SELECT * FROM news") or die(mysql_error());
while($row = mysql_fetch_array( $data ))
{
if ($row['type']==$news AND $row['id']!=$story[1] AND $row['id']!=$story[2] AND $row['id']!=$story[3] AND $row['id']!=$story[4] AND $row['id']!=$story[5])
{
if ($i<=9)
{
$i=$i+1;
setcookie($i, $row['title']);
$i=$i+1;
setcookie($i, $row['body']);
$i=$i+1;
setcookie($i, $row['link']);
}
}
}
purpose is basically let's say you have 1000 items, and 1000 users.
Let's say that user 1 decides that he doesn't want to see item (2, 5, 6, 7, etc.) anymore
So I made a table which is basically
User Item
1.......2
1.......5
etc.
so when the user does a query, it would knock out the items they don't want to see and only give a fresh result with items directed towards the end user.
Posted: Wed Sep 27, 2006 4:10 am
by Benjamin
You should really add a field to the table and called blocked or something and fill it with a boolean value.
Posted: Wed Sep 27, 2006 4:27 am
by affihq
would I have to make a column for each user with the same bool for each item, or is there a way that I can do it with just one bool column?
Posted: Wed Sep 27, 2006 4:50 am
by volka
Keep your user->item table.
You can use a LEFT/RIGHT JOIN to get the data you want.
a LEFT JOIN takes all records from the left table, if there's no matching record for the right table it is still listed once having all fields for the right table set to Null.
readit is a filter, i.e. each record there marks a news entry not to be shown? Then you want all those records from news you can't find a record in readit for. I.e. the fields for news are Null.
Code: Select all
SELECT
a.title
FROM
news as a
LEFT JOIN
readit as b
ON
b.user=1
AND a.id=b.story
WHERE
b.story IS Null
b.user=1
Here you set the userid, so this query is for the user with the id 1.
Posted: Wed Sep 27, 2006 4:44 pm
by affihq
awesome that works perfectly, now I was just wondering how I can script this into php. Never mind figured it out
