I installed a multivendor module on opencard 2, which does not have the ability to display all sellers and their products on the site itself (not the admin panel), it is not possible to look at the card of any seller and what products he has. I managed to do so that the brief information about the seller was displayed in the product card, which he added, it turned out to make a separate seller page with information about him, now on this page you need to display all of his goods.

For this you need to use three tables:

  • the first vendor stores fields with vendor id and product id,
  • second product table with product data
  • and in the third table product_description you only need to select the product name.

One vendor from the vendor table can have, for example, three products with the corresponding id, and you need to take these id's out of the product data from the product and product_description tables and write all this data into one array so that you can output all the goods of this seller on his page in a cycle .

what i did ...

 $vproducts = array(); $sql_products_data = array(); for ($i = 0; $i < $count_id; $i++) { $sql_products_data[] = $this->db->query("select product_id, image, price, name from " . DB_PREFIX . "product, " . DB_PREFIX . "product_description where " . DB_PREFIX . "product_description.product_id = " . $query['vproduct_id'] . " and " . DB_PREFIX . "product.product_id = " . $query['vproduct_id']); } foreach ($sql_products_data as $result) { if ($result) { $vproducts['prod_id'] = $result['product_id']; $vproducts['prod_image'] = $result['image']; $vproducts['prod_price'] = $result['price']; $vproducts['prod_name'] = $result['name']; } } if ($vendor_products) { foreach ($vendor_products as $result) { $data['products_list_vendor']['vproduct_id'] = $result['prod_id']; $data['products_list_vendor']['vproduct_image'] = $result['prod_image']; $data['products_list_vendor']['vproduct_price'] = $result['prod_price']; $data['products_list_vendor']['vproduct_name'] = $result['prod_name']; } } <div id="vendor_products"> <?php forech($products_list_vendor as $products_list) { ?> <p><?php echo $products_list['vproduct_id']; ?></p> <p>- - - - - - - - - -</p> <?php } ?> </div> 
  • And the errors in the code were not corrected, which I pointed out to you earlier - VenZell
  • no, no, corrected in files, and still 500 - privetsh
  • correct here too, and if my answer helped you, accept it. - VenZell
  • rechecked again, anyway, error 500 - privetsh
  • With an error of 500, I figured out by writing to the hosting, and on the seller’s page the following errors ... shopdemo.byethost32.com/index.php?route=information/… . The code remains the same which is given here - privetsh

2 answers 2

You have several problems in your code:

1.1) It is better to integrate data from several tables through JOIN

http://dev.mysql.com/doc/refman/5.7/en/join.html

 SELECT * FROM products JOIN providers ON providers.id = products.provider_id WHERE providers.status = "active" 

1.2) If there is an array, then 1 request can be made.

 // SELECT * FROM products WHERE id IN (1, 2, 3, 4, 5, 6) $idList = [1, 2, 3, 4, 5, 6]; $sql = sprintf( "SELECT * FROM products WHERE id IN (%s)", join(", ", $idList) ); 

2) For formatting query strings, it is better to use functions and not concatenation. This will greatly reduce the number of syntactic errors and not be confused in brackets.

http://php.net/manual/en/function.sprintf.php

 $sql = sprintf( "SELECT * FROM %s.%s", DB_PREFIX, $tableName ); 

3) Prepared Statements are best used to pass a parameter to the request. Not sure if your object allows it to be used.

http://php.net/manual/en/pdo.prepared-statements.php

http://php.net/manual/en/pdo.prepare.php

 // Bonus: SQL Injection safe!!! $db->prepare("SELECT * FROM products WHERE name = ?"); $db->execute($GET['name']); // or $idList = [1, 2, 3, 4, 5, 6]; $qMarks = array_fill(0, count($idList), "?"); // SELECT * FROM products WHERE id IN (?, ?, ?, ?, ?, ?) $sql = sprintf( "SELECT * FROM products WHERE id IN (%s)", join(", ", $qMarks) ); $db->prepare($sql); $db->execute($idList); 

4) It is better to separate the code and output data.

5) You still have variables jumping

$vproducts => $vendor_products

$data['products_list_vendor'] => $products_list_vendor .

Rather, this is the problem! Be careful with copy-paste.

  • Very informative, thanks, I will definitely save, but this is not exactly what I need right now! - privetsh
  • @privetsh You have another problem variables are jumping $vproducts => $vendor_products $data['products_list_vendor'] => $products_list_vendor . Rather, this is the problem! - E_p
  • @privetsh A more accurate answer, in this case, is not help, but work. You have to pay for it;). - E_p
  • With an error of 500, I figured out by writing to the hosting, and on the seller’s page the following errors ... shopdemo.byethost32.com/index.php?route=information/…. The code remains the same which is given here - privetsh

Once again, hello and many thanks to those who helped, but! It turned out to understand and most, as it turned out, everything turned out to be much easier ... Naaa a lot! So, for my situation, which is described above, the implementation is as follows: In the model file, we write such code ... (here I corrected the request itself, which was exactly what I asked you about)

 public function getProductsForVendor() { $query = $this->db->query(" select p.product_id, p.image, p.price, pd.name from ".DB_PREFIX."product p, ".DB_PREFIX."product_description pd, ".DB_PREFIX."vendor v where p.product_id= v.vproduct_id and pd.product_id = v.vproduct_id and v.vendor = ".(int)$this->request->get['vendor_id']); return $query->rows; } 

In the controller file ...

 $vproducts_data_info = $this->model_catalog_vendor->getProductsForVendor(); foreach ($vproducts_data_info as $result) { $data['vendor_products_info'][] = array( 'prod_id' => $result['product_id'], 'prod_image' => $result['image'], 'prod_price' => $result['price'], 'prod_name' => $result['name'] ); } 

And the output itself to the page ...

 <div id="vendor_products"> <?php foreach($vendor_products_info as $list) { ?> <p><?php echo $list['prod_id']; ?></p> <p><?php echo $list['prod_image']; ?></p> <p><?php echo $list['prod_price']; ?></p> <p><?php echo $list['prod_name']; ?></p> <p>- - - - - - - - - -</p> <?php } ?> </div> 

The output is painfully noticeable solely to test the operation of the model and controller. Thanks to all!