Web Design Forum: PHP Tables - Web Design Forum

Jump to content

WDF
WDF Premium Memberships Reseller Hosting
Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

PHP Tables Rate Topic: -----

#1 User is offline   Lee_K 

  • Forum Newcomer
  • Pip
  • Group: Members
  • Posts: 43
  • Joined: 19-October 11
  • Reputation: 0

Posted 09 January 2012 - 08:10 PM

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.
0

#2 User is online   Jay Gilford 

  • Web Guru
  • PipPipPipPipPip
  • Group: Members
  • Posts: 1,105
  • Joined: 11-October 09
  • Reputation: 185
  • Gender:Male
  • Experience:Web Guru
  • Area of Expertise:Web Developer

Posted 09 January 2012 - 08:26 PM

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
1

#3 User is offline   Lee_K 

  • Forum Newcomer
  • Pip
  • Group: Members
  • Posts: 43
  • Joined: 19-October 11
  • Reputation: 0

Posted 09 January 2012 - 08:47 PM

View PostJay Gilford, on 09 January 2012 - 08:26 PM, said:

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?
0

#4 User is offline   webdesigner93 

  • Web Guru
  • PipPipPipPipPip
  • Group: Members
  • Posts: 1,976
  • Joined: 22-September 09
  • Reputation: 222
  • Gender:Male
  • Experience:Web Guru
  • Area of Expertise:Web Developer

Posted 09 January 2012 - 09:32 PM

View PostLee_K, on 09 January 2012 - 08:47 PM, said:

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>";
?>


This post has been edited by webdesigner93: 09 January 2012 - 09:32 PM

1

#5 User is offline   Lee_K 

  • Forum Newcomer
  • Pip
  • Group: Members
  • Posts: 43
  • Joined: 19-October 11
  • Reputation: 0

Posted 09 January 2012 - 10:15 PM

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!

This post has been edited by Lee_K: 09 January 2012 - 10:19 PM

0

#6 User is offline   webdesigner93 

  • Web Guru
  • PipPipPipPipPip
  • Group: Members
  • Posts: 1,976
  • Joined: 22-September 09
  • Reputation: 222
  • Gender:Male
  • Experience:Web Guru
  • Area of Expertise:Web Developer

Posted 09 January 2012 - 10:30 PM

View PostLee_K, on 09 January 2012 - 10:15 PM, said:

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
1

#7 User is offline   Lee_K 

  • Forum Newcomer
  • Pip
  • Group: Members
  • Posts: 43
  • Joined: 19-October 11
  • Reputation: 0

Posted 09 January 2012 - 11:01 PM

Finally getting somewhere now! Thanks mate! Really appreciate it!
0

#8 User is online   Jay Gilford 

  • Web Guru
  • PipPipPipPipPip
  • Group: Members
  • Posts: 1,105
  • Joined: 11-October 09
  • Reputation: 185
  • Gender:Male
  • Experience:Web Guru
  • Area of Expertise:Web Developer

Posted 10 January 2012 - 12:35 AM

View PostLee_K, on 09 January 2012 - 08:47 PM, said:

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
1

#9 User is offline   Lee_K 

  • Forum Newcomer
  • Pip
  • Group: Members
  • Posts: 43
  • Joined: 19-October 11
  • Reputation: 0

Posted 10 January 2012 - 01:37 AM

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!

This post has been edited by Lee_K: 10 January 2012 - 01:51 AM

0

#10 User is offline   Samus 

  • Advanced Member
  • PipPipPip
  • Group: Members
  • Posts: 367
  • Joined: 05-August 11
  • Reputation: 27
  • Gender:Male
  • Location:Hackney, London, UK
  • Experience:Intermediate
  • Area of Expertise:Web Developer

Posted 10 January 2012 - 02:11 AM

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";
}

This post has been edited by Samus: 10 January 2012 - 02:13 AM

1

#11 User is offline   Lee_K 

  • Forum Newcomer
  • Pip
  • Group: Members
  • Posts: 43
  • Joined: 19-October 11
  • Reputation: 0

Posted 10 January 2012 - 02:56 PM

Thanks man, had to mess around a little bit got it working nicely! No doubt I shall be back with more questions soon though!
0

#12 User is offline   Lee_K 

  • Forum Newcomer
  • Pip
  • Group: Members
  • Posts: 43
  • Joined: 19-October 11
  • Reputation: 0

Posted 10 January 2012 - 06:15 PM

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?
0

#13 User is offline   Samus 

  • Advanced Member
  • PipPipPip
  • Group: Members
  • Posts: 367
  • Joined: 05-August 11
  • Reputation: 27
  • Gender:Male
  • Location:Hackney, London, UK
  • Experience:Intermediate
  • Area of Expertise:Web Developer

Posted 10 January 2012 - 08:25 PM

View PostLee_K, on 10 January 2012 - 06:15 PM, said:

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
1

#14 User is offline   Lee_K 

  • Forum Newcomer
  • Pip
  • Group: Members
  • Posts: 43
  • Joined: 19-October 11
  • Reputation: 0

Posted 10 January 2012 - 11:57 PM

So how can I order that by ASC?... Sorry i'm totally lost now!

This post has been edited by Lee_K: 11 January 2012 - 12:00 AM

0

#15 User is offline   webdesigner93 

  • Web Guru
  • PipPipPipPipPip
  • Group: Members
  • Posts: 1,976
  • Joined: 22-September 09
  • Reputation: 222
  • Gender:Male
  • Experience:Web Guru
  • Area of Expertise:Web Developer

Posted 11 January 2012 - 12:53 AM

View PostLee_K, on 10 January 2012 - 11:57 PM, said:

So how can I order that by ASC?... Sorry i'm totally lost now!

$sql = "SELECT * FROM `tablename` WHERE `COL 3` = '$COL3' ORDER BY `product_id` ASC";

0

#16 User is offline   Lee_K 

  • Forum Newcomer
  • Pip
  • Group: Members
  • Posts: 43
  • Joined: 19-October 11
  • Reputation: 0

Posted 11 January 2012 - 01:53 AM

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!

This post has been edited by Lee_K: 11 January 2012 - 01:54 AM

0

#17 User is offline   Samus 

  • Advanced Member
  • PipPipPip
  • Group: Members
  • Posts: 367
  • Joined: 05-August 11
  • Reputation: 27
  • Gender:Male
  • Location:Hackney, London, UK
  • Experience:Intermediate
  • Area of Expertise:Web Developer

Posted 11 January 2012 - 04:58 AM

View PostLee_K, on 11 January 2012 - 01:53 AM, said:

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');

This post has been edited by Samus: 11 January 2012 - 05:01 AM

1

#18 User is offline   Lee_K 

  • Forum Newcomer
  • Pip
  • Group: Members
  • Posts: 43
  • Joined: 19-October 11
  • Reputation: 0

Posted 11 January 2012 - 11:22 AM

Thanks man! Dunno why this wasn't sinking in last night! For some reason it looks a lot easier to understand today! Thanks again!
0

#19 User is offline   Lee_K 

  • Forum Newcomer
  • Pip
  • Group: Members
  • Posts: 43
  • Joined: 19-October 11
  • Reputation: 0

Posted 12 January 2012 - 12:01 AM

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?
0

#20 User is offline   webdesigner93 

  • Web Guru
  • PipPipPipPipPip
  • Group: Members
  • Posts: 1,976
  • Joined: 22-September 09
  • Reputation: 222
  • Gender:Male
  • Experience:Web Guru
  • Area of Expertise:Web Developer

Posted 12 January 2012 - 12:15 AM

View PostLee_K, on 12 January 2012 - 12:01 AM, said:

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?
0

#21 User is offline   Lee_K 

  • Forum Newcomer
  • Pip
  • Group: Members
  • Posts: 43
  • Joined: 19-October 11
  • Reputation: 0

Posted 12 January 2012 - 12:43 AM

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!
0

#22 User is offline   webdesigner93 

  • Web Guru
  • PipPipPipPipPip
  • Group: Members
  • Posts: 1,976
  • Joined: 22-September 09
  • Reputation: 222
  • Gender:Male
  • Experience:Web Guru
  • Area of Expertise:Web Developer

Posted 12 January 2012 - 12:45 AM

View PostLee_K, on 12 January 2012 - 12:43 AM, said:

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
0

#23 User is offline   Lee_K 

  • Forum Newcomer
  • Pip
  • Group: Members
  • Posts: 43
  • Joined: 19-October 11
  • Reputation: 0

Posted 12 January 2012 - 01:02 AM

Yeah but it still involves learning what to put where to get the desired outcome.
0

#24 User is offline   Lee_K 

  • Forum Newcomer
  • Pip
  • Group: Members
  • Posts: 43
  • Joined: 19-October 11
  • Reputation: 0

Posted 12 January 2012 - 06:39 PM

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>

This post has been edited by Lee_K: 12 January 2012 - 06:57 PM

0

#25 User is offline   Lee_K 

  • Forum Newcomer
  • Pip
  • Group: Members
  • Posts: 43
  • Joined: 19-October 11
  • Reputation: 0

Posted 12 January 2012 - 11:08 PM

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.

This post has been edited by Lee_K: 13 January 2012 - 12:07 AM

0

#26 User is online   rallport 

  • Web Guru
  • PipPipPipPipPip
  • Group: Members
  • Posts: 3,818
  • Joined: 03-January 10
  • Reputation: 266
  • Gender:Male
  • Location:England, UK
  • Experience:Advanced
  • Area of Expertise:Web Developer

Posted 14 January 2012 - 06:48 PM

Codeigniter and the Table class anyone?

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

0

#27 User is offline   Samus 

  • Advanced Member
  • PipPipPip
  • Group: Members
  • Posts: 367
  • Joined: 05-August 11
  • Reputation: 27
  • Gender:Male
  • Location:Hackney, London, UK
  • Experience:Intermediate
  • Area of Expertise:Web Developer

Posted 14 January 2012 - 06:51 PM

View Postrallport, on 14 January 2012 - 06:48 PM, said:

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
0

#28 User is online   rallport 

  • Web Guru
  • PipPipPipPipPip
  • Group: Members
  • Posts: 3,818
  • Joined: 03-January 10
  • Reputation: 266
  • Gender:Male
  • Location:England, UK
  • Experience:Advanced
  • Area of Expertise:Web Developer

Posted 14 January 2012 - 06:53 PM

View PostSamus, on 14 January 2012 - 06:51 PM, said:

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


Put your handbag away you touchy git :)
0

#29 User is offline   webdesigner93 

  • Web Guru
  • PipPipPipPipPip
  • Group: Members
  • Posts: 1,976
  • Joined: 22-September 09
  • Reputation: 222
  • Gender:Male
  • Experience:Web Guru
  • Area of Expertise:Web Developer

Posted 14 January 2012 - 07:02 PM

View Postrallport, on 14 January 2012 - 06:53 PM, said:

Put your handbag away you touchy git :)

sigh you people need to try decaf to much tension going on here <_<
0

#30 User is offline   Samus 

  • Advanced Member
  • PipPipPip
  • Group: Members
  • Posts: 367
  • Joined: 05-August 11
  • Reputation: 27
  • Gender:Male
  • Location:Hackney, London, UK
  • Experience:Intermediate
  • Area of Expertise:Web Developer

Posted 14 January 2012 - 07:22 PM

View Postrallport, on 14 January 2012 - 06:53 PM, said:

Put your handbag away you touchy git :)

Ladies night out. :D
0

#31 User is offline   Lee_K 

  • Forum Newcomer
  • Pip
  • Group: Members
  • Posts: 43
  • Joined: 19-October 11
  • Reputation: 0

Posted 15 January 2012 - 12:02 AM

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!

This post has been edited by Lee_K: 15 January 2012 - 12:05 AM

0

#32 User is offline   Samus 

  • Advanced Member
  • PipPipPip
  • Group: Members
  • Posts: 367
  • Joined: 05-August 11
  • Reputation: 27
  • Gender:Male
  • Location:Hackney, London, UK
  • Experience:Intermediate
  • Area of Expertise:Web Developer

Posted 15 January 2012 - 01:51 AM

View PostLee_K, on 15 January 2012 - 12:02 AM, said:

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.
0

#33 User is offline   Lee_K 

  • Forum Newcomer
  • Pip
  • Group: Members
  • Posts: 43
  • Joined: 19-October 11
  • Reputation: 0

Posted 16 January 2012 - 05:49 PM

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');

This post has been edited by Lee_K: 16 January 2012 - 05:50 PM

0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users