Stanislas Piotrowski
Stanislas Piotrowski

Reputation: 2694

trouble while checking if a number is a multiple of an other in php

I have some trouble while checking if a number is a multiple of an other. For my website, I have to do all queries in functions.

to be simple,

I have a query that looks for some informations in my database. and I do display that informations in some divs. If the number of results is a multiple of 3, it is display like I do not want :

enter image description here

What I want to do, is if, the number of result is a multiple of 3, then I add an other div but in style visibility hidden, so it would be good visual

enter image description here

For doing that I've tried to return in my function the mysql_num_rows result and to work with that number in my loop.

my function is like that

function fetchListeHome($id_cat) {
    $query = "SELECT `spb_maison_kit_detail_reference`, `spb_maison_kit_detail_superficie`, 
        `spb_maison_kit_detail_prix`, `spb_maison_kit_detail_url_img`, `spb_maison_kit_detail_id`  
        FROM `spb_maison_kit_detail`
        WHERE `spb_maison_kit_detail_id_categorie` = '{$id_cat}'";
    $result = mysql_query($query);
    $num_rows = mysql_num_rows($result);
    $result['num_rows'] = $num_rows;
    return $result;
}

if I can have the number of results, so I can check in the loop, the last iteration, and check if it is a multiple of 3, if that is the case, I can display my hidden div.

Actualy this return to me an error, so I can not work with the number of rows

below is the error displayed

Warning: Cannot use a scalar value as an array

Anykind of help will be much appreciated.

Upvotes: 0

Views: 48

Answers (2)

Kypros
Kypros

Reputation: 2986

You need to fetch the results of that query before you have access to the $result as an array with something like this:

$result = mysql_query($query);
$num_rows = mysql_num_rows($result);
$row = mysql_fetch_array($result); // add this line
$row['num_rows'] = $num_rows;
return $row;

Upvotes: 2

Kevin Lynch
Kevin Lynch

Reputation: 24703

Add an array and then return it

function fetchListeHome($id_cat) {
    $resultArray = array();
    $query = "SELECT `spb_maison_kit_detail_reference`, `spb_maison_kit_detail_superficie`, 
        `spb_maison_kit_detail_prix`, `spb_maison_kit_detail_url_img`, `spb_maison_kit_detail_id`  
        FROM `spb_maison_kit_detail`
        WHERE `spb_maison_kit_detail_id_categorie` = '{$id_cat}'";
    $result = mysql_query($query);
    $num_rows = mysql_num_rows($result);

    $resultArray array(
        'result'   => $result,
        'num_rows' => $num_rows
    )
    return $resultArray;
}

Upvotes: 0

Related Questions