tags:

views:

58

answers:

3

I am using the Select query as

SELECT id, ordering FROM `jos_menu` WHERE ordering='".$rec['ordering'] -'1' ."' AND parent = '0'

Here I need all the records whose ordering is less than 1 of the selected record's order($rec['ordering'] = getting from other select query ) when I am trying to echo the query I am not getting complete statement but getting only this -1' AND parent = '0'

here is the whole snippet

$where = ' WHERE (id = ' . implode( ' OR id = ', $cid ) . ')';//Pranav Dave Coded
    echo $selquery = "SELECT id, ordering FROM `jos_menu`".$where;          //Pranav Dave Coded
        $db->setQuery( $selquery );//Pranav Dave Coded
        $record = $db->loadAssocList(); //Pranav Dave Coded

    if ($model->orderItem($id, -1)) {
    echo "<pre>";
    print_r($model);
    /*exit;*/

    //echo $updorderup = mysql_escape_string($model->_db->_sql);//Pranav Dave Coded

        foreach($record as $rec)//Pranav Dave Coded
        {
            echo $aboverow = "SELECT id, ordering FROM `jos_menu` WHERE ordering='".$rec['ordering'] -'1' ."' AND parent = '0'";
            $db->setQuery( $aboverow );
        $above = $db->loadAssoc();
        echo "<pre>";
    print_r($above);
                    }//end of foreach
                 }//end of if

Please suggest me where I am getting wrong.....

+1  A: 

It looks like you may need to unwrap the -1 from the quotes:

WHERE ordering='".($rec['ordering'] - 1)."' AND parent = '0'";
Daniel Vassallo
+1  A: 

Why do you trying to put everything inline?
Why not to make some preparations first?
Why not to compare resulting query with sample one?
Why don't you check every step if it return proper result?

$val = $rec['ordering'] - 1;
//let's see if $cal has proper value:
echo $val."<br>";
$sql = "SELECT id, ordering FROM `jos_menu` WHERE ordering = $val AND parent = 0";
//let's see if query looks good:
echo $sql;
//let's print sampe query to compare:
echo "<br>" ;
echo "SELECT id, ordering FROM `jos_menu` WHERE ordering = 1 AND parent = 0";
Col. Shrapnel
Why DO U Ask SO Much WHY?? Are U a Coder Or Cornell or Lawyer?
OM The Eternity
Because coder is more thinking job than lawyer. Go figure
Col. Shrapnel
A: 

As Daniel said, you need to remove the quotes around the -1. Currently its trying to minus a string, which it wouldn't be happy with at all ;)

thebluefox
oh, really? ever heard of PHP type casting?
Col. Shrapnel
Of course I've heard of it, but it doesn't change the fact that taking 1 in that way doesn't work (I've just ran a series of tests to prove it).You should do all your math outside of the SQL query Parth, when I tried to remove 1 (as an int) inline like that I got a T_CONSTANT_ENCAPSED_STRING error. Let us know if thats sorts your problems.
thebluefox