Saving the ID and Amount information of the selected products to the database as json

Hello,

I have 3 variables when creating the products table
$product_name, $product_id, $number_of_product

I create the product table loop like this

<input type="checkbox" name="<?php echo $product_name; ?>[]" value="<?php echo $product_id.",".$number_of_product; ?>">

Output

<input type="checkbox" name="canaklar[]" value="4,3">
<input type="checkbox" name="lnbler[]" value="4,1">
<input type="checkbox" name="lnbler[]" value="2,2">
<input type="checkbox" name="quicler[]" value="1,20">
<input type="checkbox" name="receiver[]" value="5,7">
<input type="checkbox" name="receiver[]" value="2,7">
<input type="checkbox" name="receiver[]" value="3,9">
<input type="checkbox" name="receiver[]" value="1,9">
<input type="checkbox" name="receiver[]" value="4,16">
<input type="checkbox" name="multis[]" value="168,1">
<input type="checkbox" name="multis[]" value="162,1">
<input type="checkbox" name="multis[]" value="160,1">

When I Submit the selected products from the form, I want to save them to the database as json.

$canaklar = "{"4":3}";
$lnbler = "{"4":1,"2":2}";
$quickler = "{"1":20}";
$receiver = "{"5":"7","2":"7","3":"9","1":"9","4":"16"}";
$multis = "{"168":1,"162":1,"160":1}";

How do I get a variable to write to the database as json
or a more accurate option

Before getting to the how, could you please explain why you want to do this? What is the high level overview of what you have going on? What is the real problem you are trying to solve by doing this?

Satellite TV Antenna System I wrote a script that produces Offer
When a dealer makes an offer
If the dealer will select the products or specific products in the offer and be saved in the [purchaseorders] table

$product_name appears to be a category/type value. You should have a category table, with id (auto-increment integer primary index) and name columns. This will define the category id values. You would store the category id, rather than the category name for any data associated with a category.

$product_id values should be unique (your example data is duplicating 1, 2, and 4.) All the product information should be defined in a one table, with id (auto-increment integer primary index), category_id, name, and description columns. This will define the product id values. To get the category name for any product, you would write a JOIN query between the product table and the category table, ON the appropriate category id columns.

$number_of_product appears to be the quantity?

One of the main points of JSON, is that it is a standard data exchange format, so that different systems can easily parse/decode data sent between them. Using it within a single system, just adds two unnecessary layers of processing to what you are doing. Since the data is just being used within your system, don’t bother using JSON when storing the data in a database table.

When someone browses to and selects products and their quantities, they are adding these choices to a ‘shopping’ cart. When they are satisfied with the choices in the cart, they submit the cart to become a (pending) order.

To store this data, you should have an order table, that will hold the unique/one-time information about each order with a minimum of an id (auto-increment integer primary index) and user_id columns. The id column defines the order_id, that will be used to associate all other data back to the order it belongs to. The user_id indicates who placed the order.
If all items in the order will always be shipped to the same location, you would have a ship_to_id column in this table.

Next, you would have an order_item table, with a minimum of an id (auto-increment integer primary index), order_id, product_id, and quantity columns. You would insert a separate row in this table for each product_id that is part of an order. If each item in an order can be shipped to a separate location, you would have a ship_to_id column in this table.

You would also have a order_status table, with a minimum of an id (auto-increment integer primary index), order_id, datatime, status_id, and (probably a) memo columns. This table will track each change made to the order, such as when it was submitted as a pending order, when it was quoted, when it was accepted and became a active order, when it was invoiced, …

@phdr
The exact point is this,
Generates a quote for a dealer customer and the data is recorded in the “offerings” table, No problem here
I save this offer as json in the database so that it can be viewed again. No problem here


product_id:amount, product_id:amount, product_id:amount
So far, just thing

Problem after that
If the dealer, customer accepts the job, view the offer from the “offerings” table, will select and order all or some of the products in the offer
AND
I want to save in the “purchaseorders” table as “offerings” table

I tried to do something like this

foreach ( $_POST AS $key => $value ){
if($key == 'canaklar'){
  foreach ($value AS $value2){
  $canaklar[] = $value2;
  }
}
if($key == 'lnbler'){
  foreach ($value AS $value2){
  $lnbler[] = $value2;
  }
}
if($key == 'multiswitchler'){
  foreach ($value AS $value2){
  $multiswitchler[] = $value2;
  }
}
if($key == 'multiswitch_amplifier'){
  foreach ($value AS $value2){
  $multiswitch_amplifier[] = $value2;
  }
}
if($key == 'kablolar'){
  foreach ($value AS $value2){
  $kablolar[] = $value2;
  }
}
if($key == 'fkonnektorler'){
  foreach ($value AS $value2){
  $fkonnektorler[] = $value2;
  }
}
if($key == 'quick_konnektorler'){
  foreach ($value AS $value2){
  $quick_konnektorler[] = $value2;
  }
}
if($key == 'receiverler'){
  foreach ($value AS $value2){
  $receiverler[] = $value2;
  }
}
if($key == 'kameralar'){
  foreach ($value AS $value2){
  $kameralar[] = $value2;
  }
}
if($key == 'dvrlar'){
  foreach ($value AS $value2){
  $dvrlar[] = $value2;
  }
}
if($key == 'hddler'){
  foreach ($value AS $value2){
  $hddler[] = $value2;
  }
}
if($key == 'modulatorler'){
  foreach ($value AS $value2){
  $modulatorler[] = $value2;
  }
}
if($key == 'kamerakablolar'){
  foreach ($value AS $value2){
  $kamerakablolar[] = $value2;
  }
}
if($key == 'guc_kaynagi'){
  foreach ($value AS $value2){
  $guc_kaynagi[] = $value2;
  }
}
if($key == 'guc_jack'){
  foreach ($value AS $value2){
  $guc_jack[] = $value2;
  }
}
if($key == 'guc_bnc'){
  foreach ($value AS $value2){
  $guc_bnc[] = $value2;
  }
}
if($key == 'ayristiricilar'){
  foreach ($value AS $value2){
  $ayristiricilar[] = $value2;
  }
}
if($key == 'lokalantenler'){
  foreach ($value AS $value2){
  $lokalantenler[] = $value2;
  }
}
if($key == 'lokalantenamplifier'){
  foreach ($value AS $value2){
  $lokalantenamplifier[] = $value2;
  }
}
}

I got such a result
I want to separate the values in the array from commas and return the first value as ID to the next value as the product quantity.
OR
when viewing the offer value = “{“4”:3}” ?

I came to the coding result as follows
But I don’t know if it’s the correct coding

 foreach ( $_POST AS $key => $value ){
	if(isset($key) AND $key == 'canaklar'){
	  foreach ($value AS $value2){
		$bulundu = explode(",", $value2);
	  $canaklar[$bulundu[0]] = $bulundu[1];
	  }
	}
	if(isset($key) AND $key == 'lnbler'){
	  foreach ($value AS $value2){
		$bulundu = explode(",", $value2);
	  $lnbler[$bulundu[0]] = $bulundu[1];
	  }
	}
	if(isset($key) AND $key == 'multiswitchler'){
	  foreach ($value AS $value2){
		$bulundu = explode(",", $value2);
	  $multiswitchler[$bulundu[0]] = $bulundu[1];
	  }
	}
	if(isset($key) AND $key == 'multiswitch_amplifier'){
	  foreach ($value AS $value2){
		$bulundu = explode(",", $value2);
	  $multiswitch_amplifier[$bulundu[0]] = $bulundu[1];
	  }
	}
	if(isset($key) AND $key == 'kablolar'){
	  foreach ($value AS $value2){
		$bulundu = explode(",", $value2);
	  $kablolar[$bulundu[0]] = $bulundu[1];
	  }
	}
	if(isset($key) AND $key == 'fkonnektorler'){
	  foreach ($value AS $value2){
		$bulundu = explode(",", $value2);
	  $fkonnektorler[$bulundu[0]] = $bulundu[1];
	  }
	}
	if(isset($key) AND $key == 'quick_konnektorler'){
	  foreach ($value AS $value2){
		$bulundu = explode(",", $value2);
	  $quick_konnektorler[$bulundu[0]] = $bulundu[1];
	  }
	}
	if(isset($key) AND $key == 'receiverler'){
	  foreach ($value AS $value2){
		$bulundu = explode(",", $value2);
	  $receiverler[$bulundu[0]] = $bulundu[1];
	  }
	}
	if(isset($key) AND $key == 'kameralar'){
	  foreach ($value AS $value2){
		$bulundu = explode(",", $value2);
	  $kameralar[$bulundu[0]] = $bulundu[1];
	  }
	}
	if(isset($key) AND $key == 'dvrlar'){
	  foreach ($value AS $value2){
		$bulundu = explode(",", $value2);
	  $dvrlar[$bulundu[0]] = $bulundu[1];
	  }
	}
	if(isset($key) AND $key == 'hddler'){
	  foreach ($value AS $value2){
		$bulundu = explode(",", $value2);
	  $hddler[$bulundu[0]] = $bulundu[1];
	  }
	}
	if(isset($key) AND $key == 'modulatorler'){
	  foreach ($value AS $value2){
		$bulundu = explode(",", $value2);
	  $modulatorler[$bulundu[0]] = $bulundu[1];
	  }
	}
	if(isset($key) AND $key == 'kamerakablolar'){
	  foreach ($value AS $value2){
		$bulundu = explode(",", $value2);
	  $kamerakablolar[$bulundu[0]] = $bulundu[1];
	  }
	}
	if(isset($key) AND $key == 'guc_kaynagi'){
	  foreach ($value AS $value2){
		$bulundu = explode(",", $value2);
	  $guc_kaynagi[$bulundu[0]] = $bulundu[1];
	  }
	}
	if(isset($key) AND $key == 'guc_jack'){
	  foreach ($value AS $value2){
		$bulundu = explode(",", $value2);
	  $guc_jack[$bulundu[0]] = $bulundu[1];
	  }
	}
	if(isset($key) AND $key == 'guc_bnc'){
	  foreach ($value AS $value2){
		$bulundu = explode(",", $value2);
	  $guc_bnc[$bulundu[0]] = $bulundu[1];
	  }
	}
	if(isset($key) AND $key == 'ayristiricilar'){
	  foreach ($value AS $value2){
		$bulundu = explode(",", $value2);
	  $ayristiricilar[$bulundu[0]] = $bulundu[1];
	  }
	}
	if(isset($key) AND $key == 'lokalantenler'){
	  foreach ($value AS $value2){
		$bulundu = explode(",", $value2);
	  $lokalantenler[$bulundu[0]] = $bulundu[1];
	  }
	}
	if(isset($key) AND $key == 'lokalantenamplifier'){
	  foreach ($value AS $value2){
		$bulundu = explode(",", $value2);
	  $lokalantenamplifier[$bulundu[0]] = $bulundu[1];
	  }
	}
  }

function jsonize($data) {
  $result = array();
  foreach($data as $str) {
    $parts = explode(",", $str);
    $result[$parts[0]] = $parts[1];
  }
  return json_encode($result);
}

$canaklar = jsonize($_POST['canaklar']);
$lnbler = jsonize($_POST['lnbler']);

print_r($canaklar);
print_r($lnbler);

Writing out 19 blocks of code, one per category/type, is not how to do this. Do you think a site like Walmart, that you linked to as an example, which has thousands of categories of products, is coded like this?

The product id and quantity is all the data you need. If your product database is not setup with unique product ids, with everything in one table, you need to fix your database design.

Last coding example made my job easier
My script is not a shopping script
All of the products are the products I mentioned above, there is not more, that’s all
These recorded offers must be shown in the table when requested.

I am not professional, I’m trying to do something as an amateur
This forum taught me a lot, Thank you all, especially @ErnieAlex taught the basic structure of a website
I rearranged my script and converted it to PDO, I’m trying to make the codes more accurate and shorter with what I’ve learned.

But it is. Browsing and selecting items you want to buy, is the same as putting items into a shopping cart, i.e. a pending order, a quote, then choosing which items to actually buy and pay for, is the same as going through a checkout lane, where you can leave the things you don’t want to buy in the cart.

By hard coding these values, rather than to use a category database table, and let general-purpose code operate on the data, is taking more code, not less code.

Yes, you are right

I use the “SP Page Builder” plugin for my joomla website.
Saving all information on a sheet in a table column as json
However, it is not possible for me to do such an advanced coding for now. Maybe later
The editing I made now is much better than the first coding before
With this forum and your help

Sponsor our Newsletter | Privacy Policy | Terms of Service