There is three tables in the database :
1-stocks :
`stocks-id | stocks_name | description`
2-Selers :
`Selers-id | Selers_name | message`
3-Stock_Seller: //shows which seller has which stock to sell
`Selers-id | Selers_id`
A-The user enters a word, we should search in stocks table to find stocks which have this word in their name and fetch their ids
B- search in the stock-seller table to find which seller has these stocks to sell and fetch the seller's message
C-Search in Stocks table to find the stock's description which the seller had it to sell
D- print the message of the seller and the stocks which they have to sell like this :
Seller1: Hi we have these requested stocks, call us: 012345678
red shirt sleeve shirt for men, size large price: 23$
blue shirt...
Sellers2: Hi, Have a nice shopping, call us: 0987654321
red short sleeve shirt for men, size large Price: 23$
orange shirt ....
purple shirt ....
my codes are below: but the problem is I cannot separate each seller's message and stocks in one part and the seller's message is printed many times for each stock and the stocks of sellers printed mixed
I think I should use arrays but I don't know how
function find($name, $name1, $name2, $name3)
{
global $db;
//**A**: find stocks which have this word in their name
$query = "select * from Stoks WHERE REPLACE(`stocks_name`, '/', '')='" . $name . "'
OR REPLACE(`stocks_name`, '/', '') ='" . $name1 . "'
OR REPLACE(`stocks_name`, '/', '') ='" . $name2 . "'
OR REPLACE(`stocks_name`, '/', '') ='" . $name3 . "'";
$res = mysqli_query($db, $query);
$num = mysqli_num_rows($res);
// $row = mysqli_fetch_assoc($res);
$endresult = "";
while ($row = mysqli_fetch_assoc($res)) //For each Stoks :
{
//**B**:find available sellers
$query2 = "select distinct Selers-id from Stock_Seller WHERE stocks_id=" . $row[stocks_id];
$res2 = mysqli_query($db, $query2);
while ($row2 = mysqli_fetch_assoc($res2)) //For each seller
{
//**B**: Find sellers message
$query_Selers = "select * from Selers WHERE Selers_id=" . $row2[Selers_id];
$res_Selers = mysqli_query($db, $query_Selers);
$row_Selers = mysqli_fetch_assoc($res_Selers);
$endresult .= $row_Selers[message] . "\r\n";
//Find stocks description
$query_stocks = "select * from stocks WHERE stocks_id=" . $row[stocks_id];
$res_stocks = mysqli_query($db, $query_stocks);
$row_stocks = mysqli_fetch_assoc($res_stocks);
$endresult .= $row_stocks[description] . "\r\n";
}
}
return urlencode($endresult);
}
it prints the message of seller1 and the description of stock1 then the message of seller2 and the description of stock1 then the message of seller1 and the description of stock2 then the message of seller2 and the description of stock2 all in one message while I want each seller has a separate message with their message and stocks in it
$seller[$row2[seller_id]] .= $row_seller[message];to save the message for each seller_id in array, but i dont know how to save the stocks descriptions for each seller_id in array ?