0

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

4
  • Instead of concatenate all the messages in a single string, fill an array with messages ($endresults []= "message") Commented May 16, 2019 at 6:39
  • How should i fill an array like this : array(seller1:array(red shirt .... , blue shirt...) ,seller2: array(red shirt .... , orage shirt...) ,...) and how to print it ? Commented May 16, 2019 at 6:43
  • Populate array in PHP: ` $array = []; $array[] = [ 'seller' => 'John', 'items' => [1,2,3] ]; $array[] = [ 'seller' => 'Jack', 'items' => [1,2,3] ]; ` Commented May 16, 2019 at 6:49
  • i add $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 ? Commented May 16, 2019 at 10:21

1 Answer 1

1

Try running this below query which I have prepared as per you table structure provided above and what to find steps.
I have assumed that the word searched in red in tables for stock

SELECT s.stocks-id, s.stocks_name, s.description, sel.Selers-id, sel.Selers_name, sel.message FROM stocks AS s 
INNER JOIN stock_seller AS ss ON ss.stocks_id=s.stocks-id 
INNER JOIN selers AS sel ON sel.Selers-id = ss.Selers-id
WHERE s.stocks_name LIKE '%red%'

Let me know in comments in case of any issue.

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

4 Comments

thank you, now how can i have query on this resualt table? becouse it should print each seller's message and their stuffs in one message, separate from other seller
did you try running this query? if yes then what result did you got? can you please share that?
yes i did it returns : stock_id | stocks_name | description | Selers-id | Selers_name | message for example something like this : 1 | shirt | red long sleeve ... | 1 |John | Hi, thanks for... & 1 | shirt | red long sleeve ... | 2 | Pitter |pitters message... & 2 | skirt| red velvet... | 1 | John |Hi, thanks for... & 2 | skirt| red velvet... | 2 | Pitter |pitters message...
i wnt to send message of each seller and his stocks in one message, like : Hi, thanks for... shirt_red long sleeve & skirt_red velvet in one message and pitters message... shirt_red long sleeve & skirt_red velvet in other

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.