For examples if we take first name,last name as the attribute values and if i want to print the data as a single column then how do we do it?
8 29826
select concat(firstname,lastname) as 'name' from tablename
Hi
nelaturuk
Welcome to TSDN.
You have reached the right place for knowledge shairing.
Here you will find a vast resource of related topics and code.
Feel free to post more doubts/questions in the forum.
But before that give a try from your side and if possible try to post what/how you have approached to solve the problem.
It will help Experts in the forum in solving/underestanding your problem in a better way.
Use simple concatination and a column alias to display as a single field.
This works fine for me when only outputting one column.
I have several fields:
no, firstname, surname, position, shoots, height, weight
I need to join firstname and surname and output as a single field along with the other fields;
so the output fields would be.
no, (firstname + lastname), position, shoots, height, weight
Thanks for any advice, here is my current code, it outputs everything I want, however the name field has a value of 'Array' in each row
<?php
/**
* @version $Id: mod_roster.php 2007-07-20
* @copyright Copyright (C) 2007 Stephen Hoult - SMH WebDesign. All rights reserved.
*/
// no direct access
defined( '_VALID_MOS' ) or die( 'Restricted access' );
// Get data records from table.
$result=mysql_query("SELECT * FROM war_roster ORDER BY position DESC, surname ASC");
mysql_query("SELECT CONCAT(firstname, surname) FROM war_roster = $name");
?>
<table width="500" class="roster_border" cellpadding="20" cellspacing="1" >
<tr bgcolor="#78171e" class="table_header">
<td valign="top"><strong>#</strong></td>
<td valign="top"><strong>Name</strong></td>
<td valign="top"><strong>Position</strong></td>
<td valign="top"><strong>Birthdate</strong></td>
<td valign="top"><strong>Height</strong></td>
<td valign="top"><strong>Weight</strong></td>
<td valign="top"><strong>Shoots</strong></td>
</tr>
<?
// Make a variable "$num" with a value "0".
$num=1;
// Do while loop for out put records.
while($row=mysql_fetch_assoc($result)){
// Plus 1 at $num.
$num++;
// Use modulus by 2 in $num value and set the value of "$bg" if result equal 0 or not.
if(($num%2)!=0){
$bg="#E6D78E";
}else{
$bg="#dcc55d";
}
?>
<tr class="table_content" bgcolor="<? echo $bg; ?>">
<td><? echo $row['no']; ?></td>
<td><? echo $row .$name; ?></td>
<td><? echo $row['position']; ?></td>
<td><? echo $row['birthdate']; ?></td>
<td><? echo $row['height']; ?></td>
<td><? echo $row['weight']; ?></td>
<td><? echo $row['shoots']; ?></td>
</tr>
<?
// End while loop.
}
// Close database connection.
mysql_close();
?>
<? echo '</table>'; ?>
Fixed the problem,
Here is the fixed code if anyone is interested, also any advice on simplifying it would be appreciated. -
<?php
-
/**
-
* @version $Id: mod_roster.php 2007-07-20
-
* @copyright Copyright (C) 2007 Stephen Hoult - SMH WebDesign. All rights reserved.
-
*/
-
-
// no direct access
-
defined( '_VALID_MOS' ) or die( 'Restricted access' );
-
-
// Get data records from table.
-
-
//$result=mysql_query("SELECT * FROM war_roster ORDER BY position DESC, surname ASC");
-
-
//mysql_query("SELECT CONCAT(firstname, surname) FROM war_roster = $name");
-
-
$result=mysql_query("SELECT no, position, birthdate, height, weight, shoots, CONCAT(firstname,' ',surname) AS 'name' FROM war_roster ORDER BY position DESC, surname ASC");
-
-
?>
-
-
<table width="500" class="roster_border" cellpadding="0" cellspacing="1" >
-
<tr bgcolor="#78171e" class="table_header">
-
<td valign="top"><strong>#</strong></td>
-
<td valign="top"><strong>Name</strong></td>
-
<td valign="top"><strong>Position</strong></td>
-
<td valign="top"><strong>Birthdate</strong></td>
-
<td valign="top"><strong>Height</strong></td>
-
<td valign="top"><strong>Weight</strong></td>
-
<td valign="top"><strong>Shoots</strong></td>
-
</tr>
-
-
<?
-
// Make a variable "$num" with a value "0".
-
$num=1;
-
-
// Do while loop for out put records.
-
while($row=mysql_fetch_assoc($result)){
-
-
// Plus 1 at $num.
-
$num++;
-
-
// Use modulus by 2 in $num value and set the value of "$bg" if result equal 0 or not.
-
if(($num%2)!=0){
-
$bg="#E6D78E";
-
}else{
-
$bg="#dcc55d";
-
}
-
?>
-
<tr class="table_content" bgcolor="<? echo $bg; ?>">
-
<td><? echo $row['no']; ?></td>
-
<td class="padding" align="left"><? echo $row['name']; ?></td>
-
<td><? echo $row['position']; ?></td>
-
<td><? echo $row['birthdate']; ?></td>
-
<td><? echo $row['height']; ?></td>
-
<td><? echo $row['weight']; ?></td>
-
<td><? echo $row['shoots']; ?></td>
-
</tr>
-
-
<?
-
// End while loop.
-
}
-
-
// Close database connection.
-
mysql_close();
-
?>
-
<? echo '</table>'; ?>
-
Everything is fine. To keep the script clean remove unwanted code:).
You can use ternary operator to show iterrative row color as follows
No need for this whole code
[PHP]// Plus 1 at $num.
$num++;
// Use modulus by 2 in $num value and set the value of "$bg" if result equal 0 or not.
if(($num%2)!=0){
$bg="#E6D78E";
}else{
$bg="#dcc55d";
}[/PHP]
use this one - <tr class="table_content" bgcolor="<?=(++$num%2==0 ? "#E6D78E" : "#dcc55d")?>">
One more quick (i hope) question is;
The code I've been trying to write outputs a hockey team roster, name, number, position, etc. Here is the code in action: http://www.whitleywarriors.net/index.php?option=com_content&task=view&id=12&Itemi d=26
Eventually each player name will need to act as a hyperlink to that players player profile/bio, as seen here: http://www.newyorkrangers.com/team/roster.asp
As a newbie to myql/php how should I go about doing that?
Here is my updated code,
Thanks for your help guys. - <?php
-
/**
-
* @version $Id: mod_roster.php 2007-07-20
-
* @copyright Copyright (C) 2007 Stephen Hoult - SMH WebDesign. All rights reserved.
-
*/
-
-
// no direct access
-
defined( '_VALID_MOS' ) or die( 'Restricted access' );
-
-
// Get data records from table.
-
-
$result=mysql_query("SELECT no, position, birthdate, height, weight, shoots, CONCAT(firstname,' ',surname) AS 'name' FROM war_roster ORDER BY position DESC, surname ASC");
-
-
?>
-
-
<table width="600" class="roster_border" cellpadding="0" cellspacing="1" >
-
<tr bgcolor="#78171e" class="table_header">
-
<td valign="top"><strong>#</strong></td>
-
<td valign="top"><strong>Name</strong></td>
-
<td valign="top"><strong>Position</strong></td>
-
<td valign="top"><strong>Birthdate</strong></td>
-
<td valign="top"><strong>Height</strong></td>
-
<td valign="top"><strong>Weight</strong></td>
-
<td valign="top"><strong>Shoots</strong></td>
-
</tr>
-
-
<?
-
// Make a variable "$num" with a value "0".
-
$num=0;
-
-
// Do while loop for out put records.
-
while($row=mysql_fetch_assoc($result)){
-
-
-
?>
-
<tr class="table_content" bgcolor="<?=(++$num%2==0 ? "#E6D78E" : "#dcc55d")?>">
-
<td><? echo $row['no']; ?></td>
-
<td align="left"><? echo $row['name']; ?></td>
-
<td align="left"><? echo $row['position']; ?></td>
-
<td><? echo $row['birthdate']; ?></td>
-
<td><? echo $row['height']; ?></td>
-
<td><? echo $row['weight']; ?></td>
-
<td><? echo $row['shoots']; ?></td>
-
</tr>
-
-
<?
-
// End while loop.
-
}
-
-
// Close database connection.
-
mysql_close();
-
?>
-
<? echo '</table>'; ?>
-
Use <a> tag around the name and provide a link to profile page with id. This id will be used to fetch the palyer information on the view profile page. - <td align="left"><a href="playerprofile.php?id=<?=$row['no']?>"><? echo $row['name']; ?></a></td>
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Philip Stoev |
last post by:
Hi all,
Please tell me if any of this makes sense. Any pointers to relevant
projects/articles will be much appreciated.
Philip Stoev
http://www.stoev.org/pivot/manifest.htm
...
|
by: Mike Chirico |
last post by:
Interesting Things to Know about MySQL
Mike Chirico (mchirico@users.sourceforge.net)
Copyright (GPU Free Documentation License) 2004
Last Updated: Mon Jun 7 10:37:28 EDT 2004
The latest...
|
by: tina.boroff |
last post by:
I didn't know what to use as a title for this. Here is the scenario:
I have a op.reqedit.php page. This page a form where you input data.
In this page I have split a MySQL field. It is called...
|
by: Dave Moore |
last post by:
Hi All,
I'm putting a website together using PHP and a MySQL database. I've been
using phpMyAdmin as it makes updating the DB nice and easy. For development,
I've been using Apache, MySQL and PHP...
|
by: Steve London |
last post by:
I'm sure this has been brought up many times, but I will ask anyway.
Let's say I have 2 tables related:
Owner:
---------
o_id
o_name
Dog:
|
by: Atli |
last post by:
This is an easy to digest 12 step guide on basics of using MySQL. It's a great refresher for those who need it and it work's great for first time MySQL users.
Anyone should be able to get...
|
by: denny1824 |
last post by:
Hi everyone,
Here is my problem. I have a Table Variable (I could easily turn it into a Temp Table instead) that will sometimes have rows that are identical except for one specific column. The...
|
by: bluereign |
last post by:
Thank you for your assistance. I am a novice looking to JOIN and append or combine records from 2 current Tables into 2 new Tables named below. I have been able to JOIN Tables with the script...
|
by: murugavelmsc |
last post by:
Hi,
i develop a application with VB and mysql.
my question: in excel a1 column have a 10 values. Based on the values we get data from mysql and place it in a2 columns (corresponding row)
...
|
by: f430 |
last post by:
i was wondering if it is possible to do the combine columns into a query to make a new table.
if my original table looked like this
Part Number.....Col 1.......Col 2.....Col 3..Col 4.......Col...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| |