0

This code works:

$row = array(5,6,89,97,101);
$found = array();
$post = 89;
$count_row = count($row);
for($i = 0; $i < $count_row; $i++){
    if($row[$i]==$post){
        $found[] = $row[$i-2];
        $found[] = $row[$i-1];
        $found[] = $row[$i];
        $found[] = $row[$i+1];
        $found[] = $row[$i+2];
        var_dump($found);
    }
}

And this does not, probably doing something wrong in the mysql_fetch_array;

$found = array();
        $q = "SELECT object_id FROM wp_term_relationships WHERE term_taxonomy_id='$term_tax_id'";
        $rs = mysql_query($q) or die(mysql_error());
        $row = mysql_fetch_array($rs, MYSQL_NUM);
        $count_row = count($row);
        for($i = 0; $i < $count_row; $i++){
        print_r($row);
            if($row[$i]==$post){
                $found[] = $row[$i-2];
                $found[] = $row[$i-1];
                $found[] = $row[$i];
                $found[] = $row[$i+1];
                $found[] = $row[$i+2];
                var_dump($found);
            }
        }

Nothing is shown if post is more than 1. Anyone know a way to solve this problem?

7
  • 1
    Try print_r($row): what is the output? More: in second example I don't see $found and $post initialization... Commented Aug 29, 2011 at 12:04
  • Define "does not work", please? Commented Aug 29, 2011 at 12:06
  • 1
    So, your query is wrong or doesn't return what you're expecting I think... If you want us to help you, show us some code !!! Commented Aug 29, 2011 at 12:09
  • @Marco Added query, the query works, I've looped it with while and checked. Commented Aug 29, 2011 at 12:12
  • Any time the body of a loop is an if statement, it's wasteful, especially when the body of the if will only be entered once. If you're only interested in 5 records, fetch only those 5 records. Commented Aug 29, 2011 at 12:21

2 Answers 2

4

mysql_fetch_array() fetches one single row, which, in your case, contains exactly one column.

You should either query all rows from the result set at once (which I don't know how that works), or you should do this:

$q = "SELECT object_id FROM wp_term_relationships WHERE term_taxonomy_id='$term_tax_id'";
$rs = mysql_query($q) or die(mysql_error());
$data = array();
while ($row = mysql_fetch_array($rs, MYSQL_NUM)) {
    $data[] = $row[0];
}
for($i = 2; $i < count($data) - 2; $i++){ // adjusted boundaries
    if($data[$i]==$post){
        $found[] = $data[$i-2];
        $found[] = $data[$i-1];
        $found[] = $data[$i];
        $found[] = $data[$i+1];
        $found[] = $data[$i+2];
        var_dump($found);
    }
}

}

I as well have adjusted the boundaries: if you are interested in the range $i-2 to $i+2, you only should run from 2 to end-2.

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

2 Comments

The code works almost like I want it. Nothing is shown if the post is less than 4. do you know how to fix that?
Instead of adjusting the boundaries, do a boundary check inside the loop so if you're at $i == 0 don't add $data[$i-2] and $data[$i-1] etc.
1

The select statement should be rewritten so as to return only the values you're looking for. One way to do this is with a UNION of two selects, one returning lesser object IDs and one returning greater. You should also make use of WordPress's wpdb class. For one thing, a site admin may change the table prefix from "wp_" to something else; $wpdb->term_relationships will give the correct name for the table.

$statement = $wpdb->prepare(
   "  (SELECT object_id 
        FROM $wpdb->term_relationships 
        WHERE term_taxonomy_id= %d 
          AND object_id <= %d
        ORDER BY object_id DESC 
        LIMIT 3)
    UNION
      (SELECT object_id 
        FROM $wpdb->term_relationships 
        WHERE term_taxonomy_id= %d
          AND object_id > %d
        ORDER BY object_id ASC 
        LIMIT 2)
      ORDER BY object_id", $term_tax_id, $post, $term_tax_id, $post);
$found = $wpdb->get_results($statement);

This also has the advantage of working when there are less than two terms relationships before or after the center (the object with id $post).

2 Comments

It seems to work, but I how do I loop them? Thanks! I tried echo $found[0]['object_id'];
No need for the loop to build the result array. wpdb::get_results returns all the results as an array. By default, the items in this array are objects. Set the second arguments of get_results to one of the constants mentioned in the documentation for a different item type. In this specific example, you could instead use wpdb::get_col

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.