Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I ran mysql_query and queried the table I selected, then used mysql_fetch_assoc to put the results into an array. Then I tried to loop through that array and, basically, I ran an if statement saying that if a post was of a certain category ID, I want it to go to another array, but it just doesn't seem to work. Here's what I currently have:

$getPostType = "SELECT * FROM myTable";

$queryPostType = mysql_query($getPostType) or die("Could Not Connect: ".mysql_error());
$postTypeArray = mysql_fetch_assoc($queryPostType); 

$queryPostType1 = array();

foreach ($postTypeArray as $key => $value) {
    echo $key;
    echo $value;
    if($postTypeArray['category']===7){
        echo $key;
        echo $value;
        $queryPostType1[]=$postTypeArray[$value];
        //array_push($queryPostType1, $postTypeArray);
    }
}

It would also help to know, that each element is a row from a table with multiple columns.

so far the way im doing, and it works is:

<?php $queryPostType = mysql_query($getPostType) or die("Could Not Connect: ".mysql_error()); ?>
<?php $postTypeArray = mysql_fetch_assoc($queryPostType); ?>
<?php do{ ?>
    <?php if($postTypeArray['category'] == 7){ ?>                 
        <article>               
            <h3><strong><?php echo $postTypeArray['title']; ?></strong></h3>
            <div><?php echo $postTypeArray['html']; ?></div>
        </article>
    <?php } ?>
<?php }while($postTypeArray = mysql_fetch_assoc($queryPostType)) ?>

Where "category" is the category ID, "title" is the post title, and "html" is the actual post content.

But the only problem with that is that I do that over and over again and make a query for each category ID. Im some places I'm making as much as 12 queries but I'd like to make only one query, close my connection, loop through the data, push them into separate arrays and distribute the date from there.

Thanks again for any help.

share|improve this question
    
$queryPostType1[]=$postTypeArray[$key]; nah ? –  mpgn Sep 16 '14 at 18:43
    
Does it enter the if statement at all right now? I believe you should be checking if ( $key === 7 ). –  michaelp Sep 16 '14 at 18:49
    
@ martialdidit I tried, when i echo $queryPostType1 i get "Array to string conversion" –  user2895562 Sep 16 '14 at 18:57
    
@ michaelp does the same thing as i have now. –  user2895562 Sep 16 '14 at 18:58

3 Answers 3

Try this :

  • declare the array
  • add value to the array with $queryPostType1[$key]= $value;

code

$queryPostType1 = array(); //declare the array

$getPostType = "SELECT * FROM myTable";

$queryPostType = mysql_query($getPostType) or die("Could Not Connect: ".mysql_error());
$postTypeArray = mysql_fetch_assoc($queryPostType); 

$queryPostType1 = array();

foreach ($postTypeArray as $key => $value) {
    echo $key;
    echo $value;
    if($postTypeArray['category']===7){
        echo $key;
        echo $value;
        $queryPostType1[$key]= $value; //add value to the array
        //array_push($queryPostType1, $postTypeArray);
    }
}
share|improve this answer

You need to run a while loop for finding the next record like below.

$getPostType = "SELECT * FROM myTable";
$queryPostType = mysql_query($getPostType) or die("Could Not Connect: ".mysql_error());
$queryPostType1 = array();
while($postTypeArray = mysql_fetch_assoc($queryPostType)){
echo $key;
echo $value;
if($postTypeArray['category']===7){
echo $key;
echo $value;
$queryPostType1[]=$postTypeArray[$value];
}
} 

check this url for reference:http://www.w3schools.com/php/php_mysql_select.asp

share|improve this answer

Change from this:

    if($postTypeArray['category']===7){
        echo $key;
        echo $value;
        $queryPostType1[]=$postTypeArray[$value];
        //array_push($queryPostType1, $postTypeArray);
    }

to this:

    if($value['category']===7){
        echo $key;
        echo $value;
        $queryPostType1[]=$value;
        //array_push($queryPostType1, $postTypeArray);
    }

Alternatively, you could use $postTypeArray[$key] interchangeably with $value, as long as you are within the foreach loop

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.