• I am trying to get some data from a table. Its in an array, and then it inserts it. The problem I have is the last variable “prod_order”. I am not able to enter and save the prod_order.

    For example two records would be:
    123, 18, 13, 4
    124, 18, 11, 6

    All I can get it to do is list the order the same for each record. The product appears in category 13 and 11. 4 and 6 would be the order.

    $getem1 = $wpdb->get_results("SELECT * FROM <code>category_assoc</code> WHERE prod_id = '$lastID'");
    foreach ($getem1 as $geti) {
       $prodi = $geti->prod_order;
    
     }
    
     $wpdb->insert("category_assoc", array( "prod_id" => $lastID, "cat_id" => $cats[$i] , "prod_order" => $prodi ), array('%s', '%s', '%s' ));

Viewing 15 replies - 1 through 15 (of 15 total)
  • Hello Bloke,

    can you elaborate more on your question? I’m not really sure if I understand what you want to accomplish.

    As for your source code, there are some things that don’t seem ok to me:

    1. SELECT * FROM <code>category_assoc</code>. Do you want to select data from the category_assoc table? If yes, remove the <code> tags.

    2. Where is the $geti variable defined? You want to assign something to $prodi but you haven’t provided us with the variable $geti and it’s content so can’t really help you.

    Fix the code and paste it here again with a better explanation of your problem. Thanks

    Thread Starter Bloke

    (@bloke)

    Sorry it puts the in there when I pasted it.
    It sort of got it working. It holds keeps the values of the prod_order but not assigned to the correct record.
    It will save it as:
    123, 18, 13, 4
    124, 18, 11, 6
    instead of:
    123, 18, 13, 6
    124, 18, 11, 4

    $getem1 = $wpdb->get_results("SELECT * FROM category_assoc WHERE prod_id = '$lastID'");
    
     $array = array();
    foreach ($getem1 as $res){
        $array[] = $res->prod_order;
    }
    $wpdb->insert("category_assoc", array( "prod_id" => $lastID, "cat_id" => $cats[$i] , "prod_order" => $array[$i] ), array('%s', '%s', '%s' ));

    Format your code so I can read it please 🙂

    Thread Starter Bloke

    (@bloke)

    Ok its formatted now. I guess I need to match up the value of the prod_order to cat_id?

    The code is still incomplete. From where is $i coming? Also, if you try to echo the prod_order from the PHP, is it working? Or does it break only after inserted into the DB? Sorry but I’m a little lost here.. Also shouldn’t the prod_order be of integer type (so using %d instead of %s in your insert query)?

    Thread Starter Bloke

    (@bloke)

    The $i is something I was trying. “prod_order” => $array[$i] works but the order is not correct. And “prod_order” => $array[] throws error. So its the formatting I have wrong. I guess its not correct. But at lease this was holding the values when it insert to the database. If it should be something else let me know.

    I can change the integer type to.

    $i is a variable, it holds some value. I can’t find a definition of $i in the code snippet you posted here, that’s why I’m asking for more code so I can see what the $i variable holds.

    What is prod_order? From your code it looks like it’s a column in a category_assoc. You’ve written that the result should be some number (e.g. 6), so prod_order must be a number (looks like integer from what you’ve written). That’s why you can’t assign $array to it ($array is a PHP array, not integer).

    Thread Starter Bloke

    (@bloke)

    category_assoc has these values: cat_assoc_id, prod_id, cat_id, prod_order

    123,18,13,6 where 6 is the order. It is keeping the values but not in order. Product id 18 appear in category 13 in prod_order of 6 and it needs to remain that way. It will save it like this

    123, 18, 13, 4. Either way I need to find a way to get multiple records back and reinsert them to the database in order.

    Although I don’t fully understand it, you could move the insert into the foreach loop and use the order in each loop to insert the data in the correct order. Something like this:

    [...]
    foreach ($getem1 as $res){
        $order = $res->prod_order;
    
        $wpdb->insert("category_assoc", array( "prod_id" => $lastID, "cat_id" => $cats[$i] ,    "prod_order" => $order ), array('%d', '%d', '%d' ));
    }

    What do you think about that?

    Thread Starter Bloke

    (@bloke)

    if($edit)
    
    	$wpdb->query("DELETE FROM category_assoc WHERE prod_id = '$lastID'");
    
    	for($i= 0; $i < count($cats); $i++)
    
    	{
    
    		$wpdb->insert("category_assoc", array( "prod_id" => $lastID, "cat_id" => $cats[$i] , "prod_order" => => $order ), array('%d', '%d', '%d' ));
    
    		$wpdb->query($catCategory);
    
    	}
    Thread Starter Bloke

    (@bloke)

    Where would I put it around this code? I already have this line for the categories.

    Thread Starter Bloke

    (@bloke)

    Should it go before or after this line
    for($i= 0; $i < count($cats); $i++)

    I will elaborate what the entire script currently does and how I am trying to revise it.

    Each product id can appear in multiple categories. So there is the cat_assoc table. Each product id has (pdf) files associated with it so there is a addition_files table. When a product is edited and saved, it deletes the records associated in the categories and files tables. Then assignes the associations again. So assoc_id 123, prod_id 1, cat_id 2, prod_order 5
    becomes assoc_id 124, prod_id 1, cat_id 2, prod_order 5. The problem I am having is if 2 or more records are entered, it randomly places the prod_order value. So below in category 2 should keep order of 5 and category 3 should keep order of 6.

    Record 1: 124, 1, 2, 5
    Record 2: 125, 1, 3, 6

    Maybe there is a better way to record the product order in a table. I assume the category_assoc was the best way.

    Why do you need to delete the associated records when a product is edited? Is it done automatically and you can’t do anything about it? Or is it done on some purpose which I don’t understand?

    Thread Starter Bloke

    (@bloke)

    Yes the whole form that has the product name, title, description, images, and files associated gets updated. So even though I cam not changing a category, it has to reassign the associations with all the files and categories. This then gives it a new assoc_id number. But looking over it today, I can’t get it to work as done above. Because it loops dynamically with the categories check boxed selected. So when cat_id 2 and 3 are checked, it has no connection to the order. (Category -to- Order)relation doesn’t apply. I was manually adding values in the database yet the database dynamically doesn’t include the order. Thus wiping out my order column.

    Basically, for every category selected for this item, create a new line. Doesn’t account for the order.

    Thread Starter Bloke

    (@bloke)

    So I got to thinking, if I send the cat_id when the form is submitted, I should send the prod_order. I have this piece of code. So I added a text box next to each check box. if I can find a way to send a value for the order it could work.

    $cats = $prod->getProductCategories();
    				if(count($cats)>0)
    				{
    
    					foreach($cats as $cat)
    
    					{
    						?>
    						<tr>
    						<td><?php echo $cat['cat_name'];?></td>
    						<td><input type='checkbox' name='prodCategories[]' value='<?php echo $cat['cat_id']; ?>' <?php if($editProduct) echo $prod->isCheckSelected($cat['cat_id'], $vals['prod_id']); ?>/></td>
    						</tr>
                            <tr><td><input type="text" name="pcatorder" value=''/></td></tr>
    						<?php
    
    					}
    				}
Viewing 15 replies - 1 through 15 (of 15 total)

The topic ‘Help saving araay data to database’ is closed to new replies.