Gardener Posted September 28, 2009 Share Posted September 28, 2009 I'm attempting to make an application for Gardeners to submit their data to, its for non-commercial use and will be freeely available to all. I'm very new to php/mysql but so far using a few tutorials I have managed to get something together that will accept the info and put it in the database, from where I can also display it. Its very crude and basic but works, hopefully time and experience will improve it. Eventually the submit area will have around about 20 areas to be filled out, but some may not apply to all gardeners...so will be left blank. The obstacle I face at the present is in displaying the info, if a field is left blank then the formatting in display.php leaves "holes" in the middle of the table, by that I mean cells that have info will have a border, those without info do not have a border. Is it possible for someone to advise me on how to get around this problem? Or point me to an article that will help me understand how to as its difficult to search google when I dont know what to search for (I have already attempted an extensive search, but failed). Many thanks in advance and here are the files I have so far. process.php <?php $veg=$_POST['veg']; $sow=$_POST['sow']; $harvest=$_POST['harvest']; mysql_connect("localhost", "user", "pass") or die(mysql_error()); mysql_select_db("dbname") or die(mysql_error()); mysql_query("INSERT INTO `growing` VALUES ('$veg', '$sow', '$harvest')"); Print "Your information has been successfully added to the database."; php?> display.php <?php // Connects to your Database mysql_connect("localhost", "user", "pass") or die(mysql_error()); mysql_select_db("dbname") or die(mysql_error()); Print "<tr>"; Print "<td>Vegetable:</td><td>Seeds sown:</td><td>Harvested:</td></tr>"; $data = mysql_query("SELECT * FROM growing") or die(mysql_error()); Print "<table border cellpadding=3>"; while($info = mysql_fetch_array( $data )) { Print "<tr><td>".$info['veg'] . "</td><td>".$info['sow'] . "</td><td>".$info['harvest'] . "</td></tr>"; } Print "</table>"; ?> submit.html <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> </head> <body> <form action="process.php" method="post"> <p> </p> <table width="100%" border="0" cellspacing="0" cellpadding="0"> <tr> <td><div align="center">Vegetable:</div></td> <td><div align="center">Sow:</div></td> <td><div align="center">Harvest:</div></td> <td><div align="center"></div></td> <td><div align="center"></div></td> <td><div align="center"></div></td> <td><div align="center"></div></td> <td><div align="center"></div></td> <td><div align="center"></div></td> <td><div align="center"></div></td> </tr> <tr> <td><div align="center"> <input type="text" name="veg" /> </div></td> <td><p align="center"> <select name="sow"> <option value="sow">January</option> <option value="sow">February</option> <option value="sow">March</option> </select><br /> </p> <td><div align="center"> <input type="text" name = "harvest" /> </div></td> <td><div align="center"></div></td> <td><div align="center"></div></td> <td><div align="center"></div></td> <td><div align="center"></div></td> <td><div align="center"></div></td> <td><div align="center"></div></td> <td><div align="center"></div></td> </tr> </table> <p> <input type="submit" value="Submit"> </p> </form> </body> </html> Gardener Quote Link to comment Share on other sites More sharing options...
Dtonlinegames Posted September 28, 2009 Share Posted September 28, 2009 You're table rows are dissapearing because you have you're table structure inside the while loop. While checks for a value and displays only where theres something to return, hence no table fields <?php // Connects to your Database mysql_connect("localhost", "user", "pass") or die(mysql_error()); mysql_select_db("dbname") or die(mysql_error()); Print "<tr>"; Print "<td>Vegetable:</td><td>Seeds sown:</td><td>Harvested:</td></tr>"; $data = mysql_query("SELECT * FROM growing") or die(mysql_error()); Print "<table border cellpadding=3>"; $row=''; while($info = mysql_fetch_array( $data )) { $info['veg']=(empty($info['veg'])) ? 'None' : $info['veg']; $info['sow']=(empty($info['sow'])) ? 'None' : $info['sow']; $info['harvest']=(empty($info['harvest'])) ? 'None' : $info['harvest']; $row.="<tr><td>".$info['veg'] . "</td><td>".$info['sow'] . "</td><td>".$info['harvest'] . "</td></tr>"; } $row.="</table>"; ?> What I did was put some ternary operators in there (small if statements) that will give you're results a value even if they arent set Its not tested but should be ok hit up www.php.net and check out bits and bobs on there its the PHP dictionary Quote Link to comment Share on other sites More sharing options...
Gardener Posted September 28, 2009 Author Share Posted September 28, 2009 Thank you for your input and help, this is just displaying from the "Print "<td>Vegetable:</td><td>Seeds sown:</td><td>Harvested:</td></tr>";" line now, like this Vegetable: Seeds sown: Harvested: and no cells are appearing underneath these headings (empty or filled). Thank you for the php.net link, I'll go and take another look there and see what I can learn. Many thanks! Gardener Quote Link to comment Share on other sites More sharing options...
Gardener Posted September 28, 2009 Author Share Posted September 28, 2009 I've had a look at ternary operators on php.net and can see it sets a default value as opposed to leaving a field blank in the database. However, the code I am using, either my own or that given to me above still does not work, can anyone help or advise me further? My own code leaves unbordered spaces in the dsiplayed table, code kindly given to me above shows just the table borders, where do I go from here? Many thanks! Gardener Quote Link to comment Share on other sites More sharing options...
Gardener Posted September 29, 2009 Author Share Posted September 29, 2009 I'm really at a loss here, I've tried the tenrary operators and the also the other code for tenary ops from php.net (different code but same result apparently) and It still could not get it to work. Is it really that difficult to display a gridllike table, filled with info from an database? Gardener Quote Link to comment Share on other sites More sharing options...
Gardener Posted September 30, 2009 Author Share Posted September 30, 2009 Can I get some help, advice or otherwise here? Have I not supplied enough info or is it too difficult a question or can it not be done? I dont understand what needs to be done, where to look, what to look for for or how to approach it even. Is this being ignored because I have broken some unwritten rule or something...I really do not know what is next and could use some guidance, if I have broken some rule please let me know either here or by pm. Many thanks. Gardener Quote Link to comment Share on other sites More sharing options...
zq29 Posted September 30, 2009 Share Posted September 30, 2009 Table cells missing their borders can generally be fixed by dropping a non-breaking space in there: <td> </td> That, or using CSS to force the borders, maybe. Quote Link to comment Share on other sites More sharing options...
Gardener Posted September 30, 2009 Author Share Posted September 30, 2009 I think your assuming that I know where to put this...unfortunately I do not. If I manually add it to the mysql database then yes..it works. But how would I use the non breaking space otherwise? Many thanks Gardener Quote Link to comment Share on other sites More sharing options...
zq29 Posted September 30, 2009 Share Posted September 30, 2009 You could use a ternary operator, as previously noted, to achieve this. For example: while($info = mysql_fetch_assoc($data)) { foreach($info as $k => $v) $info[$k] = (!empty($v)) ? $v : " "; echo "<tr><td>$info[veg]</td><td>$info[sow]</td><td>$info[harvest]</td></tr>"; } Quote Link to comment Share on other sites More sharing options...
Gardener Posted September 30, 2009 Author Share Posted September 30, 2009 Thank you very much, that works perfectly. I could not see reference to this ternary on php.net, and you have used a slightly different code to the previous one "mysql_fetch_assoc" which I have not come accross before and I dont understand this "$k => $v)" either...but I'll try to look it up...thanks again. Gardener Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.