Jump to content

Concatenated data


honkmaster
Go to solution Solved by honkmaster,

Recommended Posts

Hi looking for some help with below issue I'm facing, any help would be great

I have an SQL table call Merge with the below data in

Id    StoreId    Description    
1    B013    Alana
2    B013    Sleepmotion
3    B013    Sleepmotion
4    B013    Sleepmotion
5    B013    Hyde & Sleep    
6    B017    Alana
7    B017    Isabella
8    B017    Sleepmotion
9    B017    Sleepmotion
10    B017    Sleepmotion
11    B017    Hyde & Sleep

I'm trying to get the data to display as a table with description concatenated as below


B013    Alana
            Sleepmotion
            Sleepmotion
            Sleepmotion
            Hyde & Sleep
    
B017    Alana
            Isabella
            Sleepmotion
            Sleepmotion
            Sleepmotion
            Hyde & Sleep

Edited by honkmaster
  • Like 1
Link to comment
Share on other sites

try

$res = $pdo->query("SELECT storeid
                         , description
                    FROM merge
                    ORDER BY storeid     
                    ");
foreach ($res as $r) {
    $data[$r['storeid']][] = $r['description'];
}

echo "<table>\n";
foreach ($data as $store =>$prods) {
    echo "<tr style='vertical-align: top;'><td>$store</td><td>" . join('<br>', $prods) . "</td></tr>\n";
}
echo "</table>\n";

 

  • Like 2
Link to comment
Share on other sites

Hi thank for response, I tried suggestion an I get the following errors

Notice: Undefined variable: pdo in /Applications/XAMPP/xamppfiles/htdocs/workflow/mergetest.php on line 11

Fatal error: Call to a member function query() on null in /Applications/XAMPP/xamppfiles/htdocs/workflow/mergetest.php on line 11

 

Link to comment
Share on other sites

Hi Thanks for that

I connect using 

require_once('../Connections/workflow.php');

 

<?php
# FileName="Connection_php_mysql.htm"
# Type="MYSQL"
# HTTP="true"
$hostname_workflow = "localhost";
$database_workflow = "workflow";
$username_workflow = " ";
$password_workflow = " ";
$workflow = mysql_pconnect($hostname_workflow, $username_workflow, $password_workflow) or trigger_error(mysql_error(),E_USER_ERROR); 
?>

 

Below is my database query 

 

mysql_select_db($database_workflow, $workflow);
$query_rsTest = "SELECT merge.`Description`, merge.StoreId FROM merge GROUP BY merge.StoreId";
$rsTest = mysql_query($query_rsTest, $workflow) or die(mysql_error());
$row_rsTest = mysql_fetch_assoc($rsTest);
$totalRows_rsTest = mysql_num_rows($rsTest);

Link to comment
Share on other sites

Hi Sorry for delay had to deal with couple of issues!! Thanks for response, below is the connection script.

 

<?php
$servername = "localhost";
$database = "workflow";
$username = "";
$password = "";

try {
  $conn = new PDO("mysql:host=$servername;$database=myDB", $username, $password);
  // set the PDO error mode to exception
  $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  echo "Connected successfully"; 
} catch(PDOException $e) {
  echo "Connection failed: " . $e->getMessage();
}
?>

Link to comment
Share on other sites

Hi Thank you so much for help, the connection is working now but I get the following errors

Connected successfully 
Warning: Invalid argument supplied for foreach() in /Applications/XAMPP/xamppfiles/htdocs/workflow/mergetest.php on line 18

Notice: Undefined variable: data in /Applications/XAMPP/xamppfiles/htdocs/workflow/mergetest.php on line 23

Warning: Invalid argument supplied for foreach() in /Applications/XAMPP/xamppfiles/htdocs/workflow/mergetest.php on line 23
 

<?php require_once('Connections/workflow-new.php'); ?>
<?php error_reporting(E_ALL); ?>
<?php ini_set('display_errors', '1'); ?>
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Untitled Document</title>
</head>

<body>
<?php
$res = $conn->query("SELECT StoreId
                         , Description
                    FROM merge
                    ORDER BY StoreId     
                    ");
foreach ($res as $r) {
    $data[$r['StoreId']][] = $r['Description'];
}

echo "<table>\n";
foreach ($data as $store =>$prods) {
    echo "<tr style='vertical-align: top;'><td>$store</td><td>" . join('<br>', $prods) . "</td></tr>\n";
}
echo "</table>\n";
?>
</body>
</html>

 

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.