Jump to content

mysql result laid out by column value


hyster

Recommended Posts

i have a database with a column for countrys.

ive been trying to google a way to display the results so that each country is in its own column but i can only find results for sorting the results by column.

 

 

so id like something like

 

uk usa russia france

data data data data

data data data

data data

 

 

i can do it by having a query for each country but im guessing theres a way to do it easier.

id be gratefull if some1 can point me in the right direction

thx

Link to comment
Share on other sites

I wrote a tutorial about this that should help you. The way I displayed the data was different, but you should be able to take the idea and figure it out from there. Give it a shot and let me know if you need help with it. 

 

http://thewebmason.com/tutorial-parent-child-lists/

 

The basic concept is using an ORDER in your query, and then a variable to hold the country name of the previous row.

Link to comment
Share on other sites

the 2nd part is exactly what im after.

 

big diffrence is that im pulling from 1 table . tanks_owned, then i need country and name.

country=>name.

 

the below code errors at line 10 with Call to a member function fetch_assoc()

 

(im on pain killers atm so im not thinking clearly enough :$)

 

 

 

<?php
$locations = array();
$var = "hyster";//comes from a diffrent query

$sql = "SELECT * from tanks_owned where player = '$var'";

$res=mysql_query($sql);
while ($row = $res->fetch_assoc()){
$name = $row['name'];
$country = $row['country'];
$tank_id = $row['tank_id'];
$locations[$country][$tank_id] = $row['name'];
}
?>

<h1>Our Locations</h1>
<?php
foreach($locations AS $country=>$name){
echo "<div>
<h3>{$country}</h3>
<ul>";
foreach($country AS $name){
echo "<li>{$name}</li>";
}
echo "</ul></div>";

}
?>

Link to comment
Share on other sites

mydata

INSERT INTO countrydata (country, item) VALUES
('uk', 'item 1'),
('usa', 'item 2'),
('uk', 'item 3'),
('russia', 'item 4'),
('france', 'item 5'),
('france', 'item 6'),
('usa', 'item 7'),
('uk', 'item 8'),
('france', 'item 9'),
('russia', 'item 10');

 

 

my method

$db = new mysqli(HOST,USERNAME,PASSWORD,DATABASE);

$sql= "SELECT country, GROUP_CONCAT(item ORDER BY item SEPARATOR '<br>') as items
   FROM countrydata
   GROUP BY country";
$res = $db->query($sql);

while (list($country, $items) = $res->fetch_row()) {
   echo "<div style='width:150px; padding:10px; float:left;'>
   <h4>$country</h4>$items
   </div>";
}

Link to comment
Share on other sites

im getting Call to a member function fetch_row()

if i understand the error it means theres no data

i echo'd the sql and copied into phpmyadmin and the query works

i echo'd the result and got Resource id #5

 

mysql_connect("localhost", "$username", "$password") or die(mysql_error());
mysql_select_db("$dbname") or die(mysql_error());
$sql= "SELECT country, GROUP_CONCAT(name ORDER BY country SEPARATOR '<br>') as name	
 FROM tanks_owned	
 GROUP BY country";
$result=mysql_query($sql);
while (list($country, $name) = $result->fetch_row()) {	
	 echo "<div style='width:150px; padding:10px; float:left;'>	
	 <h4>$country</h4>$name
			 </div>";
}

Link to comment
Share on other sites

cheers barand that worked a treat.

im trying to alter the sql a bit now to only pull for a certain user.

 

 

SELECT country, GROUP_CONCAT(name ORDER BY country SEPARATOR '<br>') as name

FROM tanks_owned

GROUP BY country where player = 'hyster'

 

i tried to put this into a sub query but my sql skills are basic

 

SELECT * from tanks_owned where player = 'hyster'

(SELECT country, GROUP_CONCAT(name ORDER BY country SEPARATOR '<br>') as name

FROM tanks_owned

GROUP BY country )

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.