Jump to content

[SOLVED] Seeking some advice on displaying info from mysqldb


Gardener

Recommended Posts

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

Link to comment
Share on other sites

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 :)

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

 

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.