PDA

View Full Version : Can you take part of a mysql table name and use it in a link?


ketanco
08-07-2009, 11:47 AM
Can you use part of a table name based on from what table the data is displayed at that moment? Lets say the data is being displayed from newsbbb. Can I take that bbb and use it to produce the link? so that when the user hits (more...) link, he goes to that table...Please see the last line of code in my question.

Say I have a few tables and with a statement like below, I am displaying the latest dated entry when looking at all of those tables.

$query=mysql_query(SELECT col1, col2, col3, date FROM newsaaa)
UNION
(SELECT col1, col2, col3, date FROM newsbbb)
UNION
(SELECT col1, col2, col3, date FROM newsccc)
ORDER BY date DESC
LIMIT 1


Then I want to display the values of the selected row from the selected table, such as:

while ($row = mysql_fetch_array($query)) {
echo"<p class='newstitle1'>" . $row['date']."<br/>". $row['title'] . "</p>".
"<p id=newstext1>" . $row['paragraph1']."<br/>"."<br/>";
$b=$row['id'];
echo"<a href='http://www………………..com/(xxx)*/display_(xxx)*_whole.php?id=$b'".">"."(more...)"."</a>" ; }


Please look at the last echo statement. I want to display the proper link name based on table name in the link...shown by (xxx)

Fumigator
08-07-2009, 04:15 PM
What you can do is add another column to each SELECT in your UNIONs, which is just a string. For example:


SELECT col1, col2, col3, date, 'aaa' as tblname FROM newsaaa)
UNION
(SELECT col1, col2, col3, date, 'bbb' as tblname FROM newsbbb)
UNION
(SELECT col1, col2, col3, date, 'ccc' as tblname FROM newsccc)


Now that value, 'aaa', 'bbb', or 'ccc', will be part of your resultset and you can use it just like any other column in the resultset.