0

I have two tables in my database:

1) blog_table 2) content

In blog_table I have values called postID that may or may not match up to values called id in the table content. I am wanting to know how I can write a while loop or foreach loop that will cycle through content table and perform one action if the id equals the value of postID in the blog_table and perform a different action if it doesn't.

Right now I can only get id = postID

$blog_table = $_REQUEST['blog_table'];  
$getblogtable = mysql_query("SELECT * FROM content WHERE type = '5' AND blogID = '{$_REQUEST['id']}' ORDER BY `order` ASC");

while ($row = mysql_fetch_assoc($getblogtable)) 
{
$getblogposts1 = mysql_query("SELECT postID FROM `$blog_table`");
while ($row1 = mysql_fetch_assoc($getblogposts1)) 
{
 if( $row1['postID'] == $row['id']) {
      echo "do something<br>";
 }else{
     echo "do something else<br>";
 }
} echo "<p></p>";
}

3 Answers 3

2

[Edit based on OP's comments and revised question]

$getblogposts = mysql_query("SELECT * FROM content WHERE type = '5' AND blogID = '{$_REQUEST['id']}' ORDER BY `order` ASC");
while ($row = mysql_fetch_assoc($getblogposts))
{
    $matches = mysql_query("SELECT * FROM $blog_table WHERE postID = $row['id']");
    if (mysql_num_rows($matches) > 0)
    {
        // do something
    }
    else
    {
        // do something else
    }
}

Regarding a different design, I can't say for sure that it's necessary, but I don't like running a loop of queries like this. I think one query should be enough to get everything you need in this case. Maybe if you describe your application, we could find a better query or more appropriate design.

Sign up to request clarification or add additional context in comments.

6 Comments

Okay, so I keep having the same problem. I have changed the code in my post and let me explain: let's say I have 4 values in the table content that have the same type and blogID and I have 3 values in the table blog_table...then the inside loop gets done 4 times, which will give me 3 results per iteration. I just want 1 result per iteration. So in all, when this code executes I should just get 4 results (1 results per iteration which this code should iterate 4 times if I have 4 items in my table content)
First, let me know if this describes what you're trying to do: 1. get the rows in content that are associated with a certain blogID 2. iterate through those rows looking for ones that are associated with a certain postID
1. you are correct. 2. I am wanting to iterate through those to see if the id's match postID in another table, but if they don't match I want something else to happen (which is where else comes in).
I edited to I think reflect what you're looking for. What the new code does is check to see if content.id appears at least once in the $blog_table.postID column.
Let me try that. I'm pretty alright with database design, but this one had me stumped for some reason.
|
1

Just providing an easier to see solution for you to your problem.

I suggest using inner joins which will solve the issue at hand.

For example, Something like:

SELECT * FROM content AS C INNER JOIN $blog_table AS B on B.postID = C.id

Here is a great introduction to joins (inner, left, right, full):

http://www.w3schools.com/sql/sql_join.asp

Comments

0
$blog_table = $_REQUEST['blog_table'];  
$getblogtable = mysql_query("SELECT postID FROM `$blog_table`");

while ($row = mysql_fetch_assoc($getblogtable)) 
{
  $postID = $row['postID'];
  $getblogposts1 = mysql_query("SELECT * FROM content WHERE id = '$postID' ORDER BY `order` ASC");
  while ($row1 = mysql_fetch_assoc($getblogposts1)) 
  {
     // if( $row1[id] == $postId )
     //     do something
     // else
     //     do something else
  }
}

5 Comments

do u really think your code will ever come to the else section? you are only getting contents where id = postID, have loot at your answer
It's more of an example to show how he can do one thing or do something else with the results. The example isn't limited to just his post.
Okay, so I keep having the same problem. I have changed the code in my post and let me explain: let's say I have 4 values in the table content that have the same type and blogID and I have 3 values in the table blog_table...then the inside loop gets done 4 times, which will give me 3 results per iteration. I just want 1 result per iteration. So in all, when this code executes I should just get 4 results (1 results per iteration which this code should iterate 4 times if I have 4 items in my table content)
Could I make a suggestion on changing the query at all? Something like: SELECT * FROM content AS C INNER JOIN $blog_table AS B on B.postID = C.id
yes, i like it, i will play with that and see what I can make of it. I've never used joining, but I forgot that I could do that! So does that basically eliminate the need for the inner while loop? I'm new to that type of query.

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.