Jump to content

Appending suffix to duplicate rows


karenn1

Recommended Posts

Hey everyone,

 

First off, sorry if this is maybe in the wrong forum. I wasn't sure where to post it.

 

I have a system where users can send a parcel through adding all their info to several pages. This gets recorded in rows into my database with a unique ID number and billing ID for each entry.

 

I'm now adding functionality where a user can send multiple parcels in one go. The data gets saved again with a unique ID but this time all the parcels the user wants to send gets the same billing ID. This now becomes a batch of parcels hence why the billing ID is the same.

 

For the administrator to handle these parcels (and to know that it's a batch), I have a grid already set up displaying the ID, dimensions, billing ID, etc. for all the parcels recorded. What I basically want to do now, is display the billing ID but I want to add "-1", "-2", "-3", etc. depending how many parcels there are in that batch. I want to write this into the database so that each unique ID gets the right billing ID with the new suffix attached. So this is probably a combination of PHP and PostgreSQL.

 

I tried using this query in a while loop:

 

SELECT add_session_id, COUNT(add_session_id)
from waybill
GROUP BY add_session_id
HAVING COUNT(add_session_id) > 1
ORDER BY add_session_id

 

but this returns only one entry (with the billing ID, and how many records it counted). So this doesn't work for me.

 

Can anybody please help me to implement this?

 

 

 

Thanks!

Karen

Link to comment
Share on other sites

From what I can understand, I would suggest something like the following..

 

<?php
$query = "SELECT COUNT(*) as Num FROM `table` WHERE `billing_id` = '$billing_id'";
$result = mysql_query($query) or trigger_error(mysql_error());

for($a = 0; $a < $result['Num']; $a++)
  print $billing_id . '-' . $a;

?>

 

 

Link to comment
Share on other sites

Yes, I did change the relevant values. Here's my code:

 

$query = "SELECT COUNT(*) as Num FROM public.waybill WHERE add_session_id = '$add_session_id'";
$result = pg_query($query);

for($a = 0; $a < $result['Num']; $a++) {
  		print $add_session_id . '-' . $a;
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.