Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

PHP Tables

Featured Replies

At the minute I have data within a database and can retrieve it to display inside of an HTML table. But is it possible to only display the table if certain data is available?

So basically if there is no data available from a certain column then the table row will not be displayed.

If that's possible could someone point me in the right direction.

 

Sorry if i've not explained that very well.

it all depends on what the column that's got "no data" actually contains. Your SQL would be useful too. The likely thing you will need to do is add

WHERE `column_name` IS NOT NULL

to your query

  • Author

it all depends on what the column that's got "no data" actually contains. Your SQL would be useful too. The likely thing you will need to do is add

WHERE `column_name` IS NOT NULL

to your query

 

Thanks for the reply, I understand what your saying there. How do I go about adding my HTML table into the PHP script? Just include an echo to each line?

Thanks for the reply, I understand what your saying there. How do I go about adding my HTML table into the PHP script? Just include an echo to each line?

<?php
echo "<table>";
echo "<tr>";
echo "<td></td>";
echo "</tr>";
echo "</table>";
?>

Edited by webdesigner93

  • Author

Thanks for that!

What I have is:

 

echo "<tr class='DarkPink'>";

echo "<td>'TEXT TO BE DISPLAYED'</td>";

echo "<td>'TEXT TO BE DISPLAYED'</td>";

echo "<td>'$row['COL 1']</td>";

 

echo "<td><a href="WEBSITE URL HERE" rel="nofollow"></a></td>";

 

Is that right?

 

Also, where would you recommend for learning PHP. I have picked up some of the basics and stuff but would like to take it further!

Edited by Lee_K

Thanks for that!

What I have is:

 

echo "<tr class='DarkPink'>";

echo "<td>'TEXT TO BE DISPLAYED'</td>";

echo "<td>'TEXT TO BE DISPLAYED'</td>";

echo "<td>'$row['COL 1']</td>";

 

echo "<td><a href="WEBSITE URL HERE" rel="nofollow"></a></td>";

 

Is that right?

 

Also, where would you recommend for learning PHP. I have picked up some of the basics and stuff but would like to take it further!

 

you need to escape your double quotes here

echo "<td><a href="WEBSITE URL HERE" rel="nofollow"></a></td>";

or your gonna get an error you can escape using a slash like this

echo "<td><a href=\"WEBSITE URL HERE\" rel=\"nofollow\"></a></td>";

or just use single quotes like this

echo "<td><a href='WEBSITE URL HERE' rel='nofollow'></a></td>"; 

 

thenewboston.org is a good place to start they have around 200 php videos

Thanks for the reply, I understand what your saying there. How do I go about adding my HTML table into the PHP script? Just include an echo to each line?

 

You can use PHP to generate the table, but really speaking you should save all your results as an array, and pass them to a view to iterate over. If you've never looked into MVC I would highly recommend looking into the process of it

  • Author

Everything seems to be going quite well with the way i'm doing it, and I can see the light at the end of the tunnel for what i'm trying to achieve. It's just my lack of PHP knowledge that's holding me back! But it is getting there!

 

You mentioned the: WHERE `column_name` IS NOT NULL to stop my table row from appearing...

So what I have is:

 

SELECT * FROM `tablename` WHERE `product_id` = '826' AND `COL 3` IS NOT NULL LIMIT 1

 

But it doesn't seem to work?

 

[EDIT]: If '826' didn't exist in the database would that equal to NULL? All it seems to do is leaves an empty space where that column data should be. But what I want to do is remove the full row?

 

I know these questions are probably just basic things to anyone who knows a thing or two about PHP and I do try to read up as much as I can but sometimes it's just a lot quicker to ask someone. So thanks guys!

Edited by Lee_K

You'd need to put it into a conditional statement. Basically you'd test to see if there are any rows, if there are you output the table, else you do something else like showing an error message 'no data' or whatever.

 

Something like.

 

$sql = "SELECT * FROM `tablename` WHERE `product_id` = '826' AND `COL 3` = 'something'";
$result = mysql_query($sql);
$nums = mysql_num_rows($result);
if($nums > 0) //check if there is a row (over 0 means there is)
{

while($row = mysql_fetch_array($result))
{
echo "<tr class='DarkPink'>";
echo "<td>TEXT TO BE DISPLAYED</td>";
echo "<td>TEXT TO BE DISPLAYED</td>";
echo "<td>".$row['COL 1']."</td>";
echo "<td><a href='WEBSITE URL HERE' rel='nofollow'>LINK</a></td>"
echo "</tr>";
}

}
else {
echo "no results found";
}

Edited by Samus

  • Author

Back again! Thanks for the help up to now, all has gone well so far! But here's another 1...

 

I know how to link to multiple tables to order in ASC. But what I have is something like, WHERE `COL 1` = '826'

 

But I only want that 826 to apply for 1 table, then have another number for the next table. Basically so that if '826' appeared in the 2nd table, the data wouldn't be pulled out... Does that make sense?

Back again! Thanks for the help up to now, all has gone well so far! But here's another 1...

 

I know how to link to multiple tables to order in ASC. But what I have is something like, WHERE `COL 1` = '826'

 

But I only want that 826 to apply for 1 table, then have another number for the next table. Basically so that if '826' appeared in the 2nd table, the data wouldn't be pulled out... Does that make sense?

Yeah, you'd only need to modify the query per table, you'd probs wanna make this into a function to stop repetitive code.

 

using my code snippet above:

function output_tables($COL3) 
{

$sql = "SELECT * FROM `tablename` WHERE `COL 3` = '$COL3'";
$result = mysql_query($sql);
$nums = mysql_num_rows($result);
if($nums > 0) //check if there is a row (over 0 means there is)
{

while($row = mysql_fetch_array($result))
{
echo "<tr class='DarkPink'>";
echo "<td>TEXT TO BE DISPLAYED</td>";
echo "<td>TEXT TO BE DISPLAYED</td>";
echo "<td>".$row['COL 3']."</td>";
echo "<td><a href='WEBSITE URL HERE' rel='nofollow'>LINK</a></td>"
echo "</tr>";
}

}
else {
echo "no results found";
}

}

 

Then use it like:

 

output_tables('826');
<br/>
output_tables('821');

 

where 826 & 821 are the values for COL 3

  • Author

Sorry guys but this has me totally lost now!

 

Basically what I have is:

 

<?php

 

$sql = "SELECT * FROM `Retailer` WHERE `product_id` = '826' AND `price` IS NOT NULL";

$result = mysql_query($sql);

$nums = mysql_num_rows($result);

if($nums > 0) //Check if there is a row (over 0 means there is)

{

 

while($row = mysql_fetch_array($result))

{

echo "<table>";

echo "<tr class='DarkPink'>";

echo "<td>Retailer</td>";

echo "<td>£ $row[price]</td>";

 

echo "<td><a href=WEBSITEURL rel='nofollow'></a></td>";

 

echo "</tr>";

echo "</table>";

}

 

}

else {

echo "";

}

 

?>

 

Then on the next row I want to retrieve data from the same column, but from another table, and ONLY from: WHERE `product_id` = '990' AND `price` IS NOT NULL";

 

What I don't is it to be picking out '826' from the second table aswell.

 

And then once I have my 5 rows from 5 different tables, I want to be able to order them by ASC.

 

Apologies if this is basically what you guys have already said, it's late and this thing has me well puzzled now!

Edited by Lee_K

Sorry guys but this has me totally lost now!

 

Basically what I have is:

 

<?php

 

$sql = "SELECT * FROM `Retailer` WHERE `product_id` = '826' AND `price` IS NOT NULL";

$result = mysql_query($sql);

$nums = mysql_num_rows($result);

if($nums > 0) //Check if there is a row (over 0 means there is)

{

 

while($row = mysql_fetch_array($result))

{

echo "<table>";

echo "<tr class='DarkPink'>";

echo "<td>Retailer</td>";

echo "<td>£ $row[price]</td>";

 

echo "<td><a href=WEBSITEURL rel='nofollow'></a></td>";

 

echo "</tr>";

echo "</table>";

}

 

}

else {

echo "";

}

 

?>

 

Then on the next row I want to retrieve data from the same column, but from another table, and ONLY from: WHERE `product_id` = '990' AND `price` IS NOT NULL";

 

What I don't is it to be picking out '826' from the second table aswell.

 

And then once I have my 5 rows from 5 different tables, I want to be able to order them by ASC.

 

Apologies if this is basically what you guys have already said, it's late and this thing has me well puzzled now!

Use my function above. Edit the SQL to show what webdesigner93 posted. And simply just call the functions.

 

You wouldn't need to re echo them, because the function is already doing that.

 

You'd have to include the whole written function at the top of your page, or in another file which is included/required in that page. And you just call the function five times.

 

output_tables('826');

<br/>

output_tables('824');

<br/>

output_tables('822');

<br/>

output_tables('820');

<br/>

output_tables('818');

Edited by Samus

  • Author

Still struggling with this sadly! I know it's frustrating when your trying to help someone and they just can't see the obvious but I just can't! Sorry lol.

 

How come I can't make function output_tables($COL3) a number where COL3 is? I copied the code as above and edited it to suit but guess i've gone wrong somewhere?

Still struggling with this sadly! I know it's frustrating when your trying to help someone and they just can't see the obvious but I just can't! Sorry lol.

 

How come I can't make function output_tables($COL3) a number where COL3 is? I copied the code as above and edited it to suit but guess i've gone wrong somewhere?

mmm u mean make output_tables out put an integer value instead of a string?

Scrap that last message! I've managed to fix it! Wow learning HTML and CSS was so much easier than trying to learn PHP! But i'm determined!

despite what some people say html and css is not a real programming language but a way of laying out things, so thats why haha

  • Author

I think there might be something wrong with the code above.

I can get it working to display 1 row, using 1 of the functions but what I need is to display 5 different rows from 5 different tables. Which would mean using multiple functions? And for some reason when I try to use more than 1, the page just stays blank>

Edited by Lee_K

  • Author

Hmmm the ORDER ASC just works within the 1 function, what I want to be able to do is ORDER ASC within all the functions on the page, basically grouping them together.

Edited by Lee_K

Codeigniter and the Table class anyone?

 

$this->load->library('table');
$query = $this->db->query("SELECT * FROM the_table");
echo $this->table->generate($query); 

That'd be ideal if the OP wasn't just getting into this

  • Author

Yeah I think i'm going to have to look into PHP on a bigger scale if I want this to work exactly how I want it too lol. So how long do you think it'll take me? A week? two? lol. I'll have no hair left!

 

Thanks for the help in the meantime though, I have actually learnt a fair bit from this!

Edited by Lee_K

Yeah I think i'm going to have to look into PHP on a bigger scale if I want this to work exactly how I want it too lol. So how long do you think it'll take me? A week? two? lol. I'll have no hair left!

 

Thanks for the help in the meantime though, I have actually learnt a fair bit from this!

Depends on your time, depends on your resources, depends on your dedication and also depends how fast you're able to understand the concept of particular features.

  • Author

Ok, I think I may have found another way of achieving what I want using the code above but edited slightly. I'm pretty sure it's not a lot to change so if anyone could tell me how it'd be much appreciated...

 

Basically how can I alter this to join multiple tables together and also display it using the LIKE term:

 

function output_tables($COL3) 
{

$sql = "SELECT * FROM `tables 1. 2 and 3` WHERE `COL 3` = '$COL3'";  <----- I want this to be WHERE `COL 3` LIKE 'xyz'
$result = mysql_query($sql);
$nums = mysql_num_rows($result);
if($nums > 0) //check if there is a row (over 0 means there is)
{

while($row = mysql_fetch_array($result))
{
echo "<tr class='DarkPink'>";
echo "<td>TEXT TO BE DISPLAYED</td>";
echo "<td>TEXT TO BE DISPLAYED</td>";
echo "<td>".$row['COL 3']."</td>";
echo "<td><a href='WEBSITE URL HERE' rel='nofollow'>LINK</a></td>"
echo "</tr>";
}

}
else {
echo "no results found";
}

}

 

So then I can recall it sort of like:

 

output_tables('LIKE XYZ');

Edited by Lee_K

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.