January 9, 201214 yr 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.
January 9, 201214 yr 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
January 9, 201214 yr 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?
January 9, 201214 yr 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 January 9, 201214 yr by webdesigner93
January 9, 201214 yr 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 January 9, 201214 yr by Lee_K
January 9, 201214 yr 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
January 10, 201214 yr 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
January 10, 201214 yr 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 January 10, 201214 yr by Lee_K
January 10, 201214 yr 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 January 10, 201214 yr by Samus
January 10, 201214 yr Author Thanks man, had to mess around a little bit got it working nicely! No doubt I shall be back with more questions soon though!
January 10, 201214 yr 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?
January 10, 201214 yr 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
January 10, 201214 yr Author So how can I order that by ASC?... Sorry i'm totally lost now! Edited January 11, 201214 yr by Lee_K
January 11, 201214 yr 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";
January 11, 201214 yr 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 January 11, 201214 yr by Lee_K
January 11, 201214 yr 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 January 11, 201214 yr by Samus
January 11, 201214 yr Author Thanks man! Dunno why this wasn't sinking in last night! For some reason it looks a lot easier to understand today! Thanks again!
January 12, 201214 yr 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?
January 12, 201214 yr 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?
January 12, 201214 yr Author 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!
January 12, 201214 yr 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
January 12, 201214 yr Author Yeah but it still involves learning what to put where to get the desired outcome.
January 12, 201214 yr 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 January 12, 201214 yr by Lee_K
January 12, 201214 yr 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 January 13, 201214 yr by Lee_K
January 14, 201214 yr 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
January 14, 201214 yr Put your handbag away you touchy git sigh you people need to try decaf to much tension going on here
January 15, 201214 yr 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 January 15, 201214 yr by Lee_K
January 15, 201214 yr 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.
January 16, 201214 yr 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 January 16, 201214 yr by Lee_K
Create an account or sign in to comment