Jump to content

PHP Displaying blanks.


fxuup

Recommended Posts

I'm trying to echo data from phpMyadmin, but i want it to only echo data that doesn't have a blank space.

view the image below please.

help.jpg

 

I'm trying to display the data but i don't want to display the empty data either. If anyone has any ideas on how i could do this it would be greatly appreciated.

 

My code is below. Thank you.

 

<div id="content">
<h2>Inventory</h2>
<form name="weapons" method="POST" action="<?php echo $_SERVER['php_self']; ?>">
  <br>
  <?
  $swords = $_POST['weapons'];

if(!empty($swords)){
echo "You equipped a " .$swords;
$set = "UPDATE `users` SET `weapon` = '$swords' WHERE `username`='$username'";
mysql_query($set);
}
  
echo '
<table border="0" id="text">
<tr>
<td width="235px" id="text2">
Swords
</td>
</table>
';
?>
<table border="0" id="text3">

<?

$display = mysql_query("SELECT `swords` FROM `inventory` WHERE `username`='$username'");

while($rowz = mysql_fetch_array($display))
  {
  
echo '

<tr>
<td id="text3">
<input type="radio" name="swords" size="1" value="' .$rowz['swords']. '">' .$rowz['swords']. '<br>
</td>
</tr>

';

?>
</table>
<br>
<input type="submit" name="submit" value="Equip">
  <br>
  <?
$bows = $_POST['bows'];
  if(!empty($bows)){
echo "You equipped a " .$bows;
$set2 = "UPDATE `users` SET `weapon` = '$bows' WHERE `username`='$username'";
mysql_query($set2);
}
  
echo '
<table border="0" id="text">
<tr>
<td width="235px" id="text2">
Axes
</tr>
</table>
';
?>
<table border="0" id="text3">

<?

$display2 = mysql_query("SELECT `axes` FROM `inventory` WHERE `username`='$username'");
while($rowzz = mysql_fetch_array($display2))
  {
echo '

<tr>
<td id="text3">
<input type="radio" name="axes" size="1" value="' .$rowzz['axes']. '">' .$rowzz['axes']. '<br>
</td>
</tr>
';
}
?>
</table>
<br>
<input type="submit" name="submit" value="Equip">
  <br>
</div>

post-127908-13482403195842_thumb.jpg

Link to comment
Share on other sites

Your database table is laid out like a spreadsheet. That wastes storage, takes more code, and more complicated queries to manage or retrieve the data.

 

The inventory table should only have id, user_id, and weapon_id columns (and perhaps a quantity column if you can have more than one of each weapon.) You should have a separate table that holds the definition of all the weapons. It would have the id, type, and name (and any other characteristics for each weapon.) The inventory table would only have rows for actual weapons in the inventory. When a user adds a weapon to his inventory, you would add a row to the inventory table (or increase his quantity of that weapon if he can have more than one and he already has at least one.) When a user looses/uses a weapon, you would delete the row for that weapon (or decrease his quantity of the weapon if he had more than one to start with.)

 

Doing this will save a TON of code. All the repeated code you posted, that only varies in the weapon type, can be replaced by one query and a loop that outputs the appropriate weapon type heading every time the weapon type changes.

Link to comment
Share on other sites

Thank you guys very much! Im only a amature when it comes to PHP, HTML, and MySQL coding. Ive been coding for about 4 months now. PFMaBiSmAd so your saying in the inventory it should put quantities instead of the weapons so like the top would say DAGGER and the values with be 2 as i have two daggers?

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.