tags:

views:

101

answers:

2

i have a

Book ID Array

    Array
    (
        [0] => 61
        [1] => 72
        [2] => 78
        [3] => 100
        [4] => 102
    )

*now from another table table_bookPrice where price filed is given i want that select all price from table_bookPrice where book id is in given array (book ID Array) and if price is not mentioned in table_bookPrice field then it would be automatic 500*

what will be exact query

so the array i got is like this

Book Price Array

    Array
    (
        [0] => 150
        [1] => 100
        [2] => 500 ( not mentioned in table, so it is 500)
        [3] => 300
        [4] => 200
    )
A: 

Here's the test database I built for your problem:

mysql> select  * from table_bookPrice;
+----+-------+--------+
| id | price | bookid |
+----+-------+--------+
|  1 |   150 |     61 |
|  2 |   100 |     72 |
|  3 |   300 |    100 |
|  4 |   200 |    102 |
+----+-------+--------+
4 rows in set (0.00 sec)

Here's the PHP code:

<?php

$books=array(61,72,78,100,102);

// establish an assoc array of bookid => default_price
$prices=array();
foreach($books as $bookid){
    $prices["$bookid"]=500;
}

// build query to select all prices stored in database
$bookids=implode(', ',$books);
mysql_connect('localhost','aj','nothing') or die('unable to connect!');
mysql_select_db('books') or die('unable to select db!');
$stmt="SELECT bp.bookid, bp.price FROM table_bookPrice bp WHERE bp.bookid IN ($bookids)";
$res=mysql_query($stmt);
while( ($rec= mysql_fetch_assoc($res)) ){
    $idstr=(string)$rec['bookid'];
    $prices[$idstr]=$rec['price'];
}

print_r($prices);

?>

This outputs:

Array
(
    [61] => 150
    [72] => 100
    [78] => 500
    [100] => 300
    [102] => 200
)
AJ
`but if there is no row in table_bookPrice corresponding book id then it should be 500 `,then what will be query
diEcho
@I Like PHP right you are...changed my answer. give it a try now.
AJ
but there is no table of table_books , there is just an array of table_book which contain id of books(that array comes from some method) so it is no possible to join one array and one table
diEcho
@I Like PHP...ah, ok. changed my answer again. hope it works for you this way now.
AJ
+1  A: 

I am at work so could not test or compile it, but I hope my logic is understandable.

Not sure if this will work but something along these lines

$book_price_array = array(); //contents to be added.

// loop through the array an examine its price by querying your table.
foreach ($book_id_array as $key => $value) {
   $price = mysql_query("SELECT price FROM table_bookPrice 
                                     WHERE book_id = {$value}");
   // there is a price, set the price.
   if ($price > 0 && $price != NULL)  $book_price_array[$key] = $price;

   // there is no price, set the default price
   else  $book_price_array[$key] = 500; 
}
Anthony Forloney
Why did you comment out part of your query in the else clause? Clearly you *didn't* have time to test this...
AJ
Clearly you *didn't* read what I had put above? I commented it out because he did not want to update the table with a new value, just his array.
Anthony Forloney
it results a new array where value of array is book id if row exist in table_bookPrice and if row does not exist then it's value is 500;i need that if row exist then value should be price not the book id($value)and one more thing`$book_price_array = new array();` gives error new keyword is not used in php( as i know)
diEcho
I updated my answer, I removed the `new` keyword and I added a variable `$price` that is set to the price of the book_id, if it exists it is added to the `$book_price_array`. Also I added to see if the value `NULL` was returned from the MySQL query
Anthony Forloney
Thank You Sir, Now one little quetion,if i want array which key is book id and value is book price ,then how it is possible in above queryanother method i array_combine() ( i know)
diEcho
Check out my edit, you can use that function after the loop.
Anthony Forloney
i said not using array_combine() in last line on my comment, anyways i got the answer myself we can use`$book_price_array[$value]` instead of `$book_price_array[$key]`
diEcho
Ah, didn't see that but I'm glad you figured it out
Anthony Forloney