0

I need an array have 6 index ,
How to select row and check if not in an array push into? if in array then select next row till the array have 6 index

$data = array();

  for ($ii=0; $ii < 6; $ii++) { 
    $sql = "SELECT * FROM article_category WHERE category_id = :category_id ORDER BY id DESC";
    $stmt = $connect_db_article->prepare($sql);
    $stmt->bindValue(':category_id', $category_id_array[$i]);
    $stmt->execute();
    $article_category_row = $stmt->fetch(PDO::FETCH_ASSOC);
    if (!in_array($article_category_row['article_id'], $data)) {
      array_push($data, $article_category_row['article_id']);

    }
  }
2
  • 1
    I'm sorry, but I don't understand the question. Could you try to clarify "get newest row and check if in array select next continue to find not in array and push into array" a little? Commented Oct 4, 2014 at 10:40
  • Thanks for reply, I update the question, I tried to find a way push array till have 6 index, each time get one row and check isset in array or not, if not push into, if duplicate in array then back to select next row Commented Oct 4, 2014 at 10:45

3 Answers 3

1

You can just loop until sizeof($data)==6, something like;

$data = array();

$sql = "SELECT * FROM article_category WHERE category_id = :category_id ORDER BY id DESC";
$stmt = $connect_db_article->prepare($sql);
$stmt->bindValue(':category_id', $category_id_array[$i]);
$stmt->execute();
while(sizeof($data) < 6 && $article_category_row = $stmt->fetch(PDO::FETCH_ASSOC)) {
  if (!in_array($article_category_row['article_id'], $data))
    array_push($data, $article_category_row['article_id']);
}
Sign up to request clarification or add additional context in comments.

2 Comments

but how to let sql select next row? this seems keep select same one
@user1775888 Hmm...? $stmt->fetch() should always fetch the next row if one is available. Since it's called in a loop it should fetch all rows in order.
1


You don't need to re execute your query at each time, you will always have only one 'article_id' in your $data array.
Also you have to secure your code with try, catch()
You can try with this code :

$data = array();
$maxRow = 6;
$cpt = 0 ;
$sql = "SELECT * FROM article_category WHERE category_id = :category_id ORDER BY id DESC";
try {
    $stmt = $connect_db_article->prepare($sql);
    $stmt->bindValue(':category_id', $category_id_array[$i]);
    $stmt->execute();
    while($article_category_row = $stmt->fetch(PDO::FETCH_ASSOC) && $cpt < $maxRow) {
        if (!in_array($article_category_row['article_id'], $data)) {
            array_push($data, $article_category_row['article_id']);
            $cpt++;
        }
    }
}catch(PDOException $pdoException) {
    print $pdoException->getMessage();
}

1 Comment

but how to let sql select next row? this seems keep select same one
0

also you can delegate this treatment to your MySql Server with this sql query

$sql = "SELECT DISTINCT(article_id) FROM article_category WHERE category_id = :category_id ORDER BY id DESC LIMIT 6";

hope it help

Comments

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.