help in DSS

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
zplits
Forum Contributor
Posts: 158
Joined: Sun Aug 03, 2008 8:59 pm

Re: help in DSS

Post by zplits »

Hello sir stryks. Good day. :)
Should i create a new table?
namely:
tbl_measure_units
- UNIT_PK
- unitName

Why do we need to create a new table sir? It's just a measurement. How will it be used elsewhere? I mean if i stick in a drop-down item name with kilogram(s) and it will store a value of 1 in the database. How is it going to differ when i use varchar than int?

If you have some time, we can twiddla so that we'll better understand each other.
User avatar
zplits
Forum Contributor
Posts: 158
Joined: Sun Aug 03, 2008 8:59 pm

Re: help in DSS

Post by zplits »

Here is the code for adding record into the database as well as adding checkbox at the beginning and edit button.

Code: Select all

<?php 
$query = 'Select * from tbl_ingredient';
$result = mysql_query($query) or die('Error in Query');
 
        
if(mysql_num_rows($result)>0){
    while($row = mysql_fetch_row($result)){
        echo "<table width=\"720\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" class=\"gridData\" onMouseOver=\"this.style.backgroundColor='#EDE6D4'\"; onMouseOut=\"this.style.backgroundColor='transparent'\">";
        echo "<tr align = \"center\" class=\"gridRow\">";
        echo "<td width = \"70\"><input type=\"checkbox\" name=\"removeid[]\" id=\"checkbox\" /></td>";
        echo "<td width = \"105\">".$row[1]."</td>";
        echo "<td width = \"300\">".$row[2]."</br></td>";
        echo "<td width = \"50\">".$row[4]."</br></td>";
        echo "<td width = \"100\">".$row[3]."</br></td>";
        echo "<td width = \"59\"><input name=\"Reset\" type=\"submit\" class=\"gridButtons\" id=\"button\" value=\"Edit\" onmouseover=\"this.style.color = '#000000'\" onmouseout=\"this.style.color = '#666666'\"/></td>";
                //echo $row[2]." </br>";
        echo "</tr>";
        echo "</table>";
            }
        }
else{
    echo 'No rows found';
}?>
But the edit button and checkbox aren't function. I don't know how to do it
User avatar
Stryks
Forum Regular
Posts: 746
Joined: Wed Jan 14, 2004 5:06 pm

Re: help in DSS

Post by Stryks »

Well .. there are a few things that I would recommend that you do differently ... just so that things are easier to read. These aren't hard and fast ... just practices that I usually do to keep things readable.

Firstly, try not to use wildcard (*) selects.

Code: Select all

 
$query = 'Select I_PK, code, ingredientName, measure_unit, units_per_item from tbl_ingredient';
 
// is a better practice than
 
$query = 'Select * from tbl_ingredient';
 
There's a thread around somewhere that indicates that it's faster (especially if you're not pulling all cols), but the main reason I did it this way is because when I'm coding, I can look up to the query and know what columns I am working with.

Then, instead of mysql_fetch_row(), I'd use mysql_fetch_assoc(). That way, you can refer to each piece of data by name instead of index. e.g.

Code: Select all

echo $row['code'];
 
// instead of 
 
echo $row[1];
To make the button and checkbox ideas work, you need to let each button know which item they belong to. So, the following will get you moving in the right direction.

Code: Select all

<input type=\"checkbox\" name=\"removeid[]\" id=\"checkbox\" value=\"{$row['I_PK']}\" />
 
But a bigger issue is that you seem to be using individual tables for each row of data. You shouldn't need to do that, so you can just move the open and code table tags outside of the while() loop.

So ...

Code: Select all

echo "<table width=\"720\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" class=\"gridData\" onMouseOver=\"this.style.backgroundColor='#EDE6D4'\"; onMouseOut=\"this.style.backgroundColor='transparent'\">";
    while($row = mysql_fetch_row($result)){
        echo "<tr align = \"center\" class=\"gridRow\">";
    
       // snip
 
        echo "</tr>";
   }
        echo "</table>";
        }
 


Then the only comment I have to make is that ... well .. I dont know about anyone else, but I never understand why people echo lines of HTML.

Why not ...

Code: Select all

<?php
while($row = mysql_fetch_row($result)) {
?>
<tr align = "center" class="gridRow">
   <td width = "70"><input type="checkbox" name="removeid[]" id="checkbox" value="<?php echo $row['I_PK']; ?>" /></td>
</tr>
<?php
}
?>
You don't need to mangle your HTML this way, plus it indents properly when you go into source view. And it's otherwise just far easier to view ... especially if you have an editor with code coloring. The PHP sections just jump right out at you.

Anyhow ... just food for thought.
User avatar
zplits
Forum Contributor
Posts: 158
Joined: Sun Aug 03, 2008 8:59 pm

Re: help in DSS

Post by zplits »

How about deleting those rows which was checked? What will be the code for it? Thank you very much. And also the edit button sir, Please help me, you know I'm chasing time, i thought the deadline was on sept 27. but that was wrong, the deadline will be sept 19. So please, help me, I need your help sir stryks. I'm asking it now, because i know you're also busy.

I'm sorry for being mean, but i really need your help.
User avatar
Stryks
Forum Regular
Posts: 746
Joined: Wed Jan 14, 2004 5:06 pm

Re: help in DSS

Post by Stryks »

If you need to get the checkbox values from the page your form submits to, you should be able to find them in $_POST['removeid'].

As in ...

Code: Select all

 
foreach($_POST['removeid'] as $rem_item_id) {
   $sql = "DELETE * FROM tbl_ingredients WHERE I_PK = $rem_item_id";
   // etc.
}
 
Of course, you'll want to make sure it was the delete button that was pressed. Also, you might want to prompt the user before the items are deleted, instead of just removing them right away.
Post Reply