Jump to content

Dynamic Table Inaccurate Results [Solved]


tet3828

Recommended Posts

Here is a tricky one. At least I think.

My script currently returns database search results. The information is displayed as a table with a thumbnail, item Name ect. Because this information is in a table; the results are stacked upon eachother vertically.

This is going to cause a problem for the user. I tried to come up with a loop dohickey that will display each table inside of a dynamic table so the results can be displayed within a cell. I gave it a shot and tried many variations of it but no luck.

where has my script gone wrong?
[code]
     noresults = mysql_num_rows($result);
     $columns =  3;
     $rows = $noresults / $columns;
     $rows = ceil($rows);
     $i = 0;

     print ('<table width="100%" border="1" id="content_list">');
           while($row %3[/code]
Link to comment
Share on other sites

if you want a table then Read the information Below.

Lets say you have a small MySQL Table, called users
With fields set as follows

id        int(50) primary, auto_increment
name    varchar(150)
email    varchar(150)
pwd    char(32)

Now you want to show the results for all this, except the pwd (password)

I would do the PHP code as follows

[code]
<?php
DBConnect(); //I have a function to connect to the DB, You should connect before running queries

$query1 = "SELECT * FROM users";
$result1 = mysql_query($query1);

if($result1){

if(mysql_num_rows($result1) != 0){

echo "<table>\n"
."<tr>\n"
."<th>ID Number</th>\n"
."<th>Name</th>\n"
."<th>Email</th>\n"
."</tr>\n";

while($row1 = mysql_fetch_assoc($result1)){
echo "<tr>\n"
."<td>".$row1["id"]."</td>\n"
."<td>".$row1["name"]."</td>\n"
."<td>".$row1["email"]."</td>\n"
."</tr>\n";
}
echo "</table>\n";
}else{
echo "I am afraid no results have been found<br />\n";
}
}else{
echo "There has been an error, please try again later<br />\n";
}


?>
[/code]


With this information, you can work in your query and results

Any problems, let us know
Link to comment
Share on other sites

Thanks once again for the response. it's a way better approach to my problem.
I soppose I will be able to intergrate into my script but I'll need a bit more help
comprehending this script. I understand the:

[code]
<tr>  </tr>
<td> </td>
[/code]
concept. of course it signififyies the opening and closing of a row and columns

but what is the function of:
[code]
<th> </th>
and the \n ???
[/code]
note I am researching my question on google.
but nothing beats a personal break down from a php whiz.
thanks. hope someone can help.
Link to comment
Share on other sites

I gave the suggested script my best shot and Im getting mysql errors:
The script is no longer displaying results.
any suggestions?

[code]
if ($cat  != "nocat") {

$qry = "SELECT itemName,itemId,itemPrice,itemSmall FROM `products` WHERE itemCat='".$_POST['cat']."'";
$result = mysql_query($qry) or die(mysql_error());
$amount = mysql_num_rows($result);
echo "<br /><br />";}


while($row = mysql_fetch_array($result)) {
echo "<table>\n"
."<tr>\n"
."<th>Item Picture</th>\n"
."<th>Name</th>\n"
."<th>Price</th>\n"
."<th>Edit Link</th>\n"
."</tr>\n";

while($row1 = mysql_fetch_assoc($result1)){
echo "<tr>\n"
."<td><center><img src=\"".$row['itemSmall']."\" /></center></td>\n"
."<td>".$row['itemName']."</td>\n"
."<td>".$row['itemPrice']."</td>\n"
."<td><a href=/*********.php?id=".$row['itemId']." />Edit This Item</td>\n"
."</tr>\n";
}
echo "</table>\n";
}}
[/code]
Link to comment
Share on other sites

The last reply seems to be on the right track. however my search results aren't displaying properly. its only show one result.
lets say I search for items in the holiday catagory. I know for a fact I have 2 items where itemCat = "Holiday" but only one is showing. why would this happen?

the code:

[code]
while($row = mysql_fetch_array($result)) {
echo "<table border=3>\n"
."<tr>\n"
."<th>Item Picture</th>\n"
."<th>Name</th>\n"
."<th>ID #</th>\n"
."<th>Catagory</th>\n"
."<th>Price</th>\n"
."<th>Edit Link</th>\n"
."</tr>\n";

while($row1 = mysql_fetch_assoc($result)){
echo "<tr>\n"
."<td><center><img src=\"".$row['itemSmall']."\" height=\"75\" width=\"75\"  /></center></td>\n"
."<td>".$row['itemName']."</td>\n"
."<td>".$row['itemId']."</td>\n"
."<td>".$row['itemCat']."</td>\n"
."<td>".$row['itemPrice']."</td>\n"
."<td><a href=/shell/data/edit.php?id=".$row['itemId']." />Edit This Item</td>\n"
."</tr>\n";
}

}}
echo "</table>\n";

[/code]
Link to comment
Share on other sites

The $result1 was a typeo. I've changed it to $result if you look at my last script posting you'll see the change. I didn't post the script right away. here it is again:


[code]
while($row = mysql_fetch_array($result)) {   \\\ I somewhat understand the loop concept but, I don't understand the condition here.....

echo "<table border=3>\n"
."<tr>\n"
."<th>Item Picture</th>\n"
."<th>Name</th>\n"
."<th>ID #</th>\n"
."<th>Catagory</th>\n"
."<th>Price</th>\n"
."<th>Edit Link</th>\n"
."</tr>\n";

while($row1 = mysql_fetch_assoc($result)){           \\\ Nor here.
echo "<tr>\n"
."<td><center><img src=\"".$row['itemSmall']."\" height=\"75\" width=\"75\"  /></center></td>\n"
."<td>".$row['itemName']."</td>\n"
."<td>".$row['itemId']."</td>\n"
."<td>".$row['itemCat']."</td>\n"
."<td>".$row['itemPrice']."</td>\n"
."<td><a href=/****/****/****.php?id=".$row['itemId']." />Edit This Item</td>\n"
."</tr>\n";
}

}}
echo "</table>\n";

[/code]

Oh boy I am a forum regular now....ugh that happened quick.
Link to comment
Share on other sites

I dont understand why you have two whiles... Try this please:

[code]<?php

echo "<table border=3>\n"
."<tr>\n"
."<th>Item Picture</th>\n"
."<th>Name</th>\n"
."<th>ID #</th>\n"
."<th>Catagory</th>\n"
."<th>Price</th>\n"
."<th>Edit Link</th>\n"
."</tr>\n";

while($row = mysql_fetch_array($result))
{
echo "<tr>\n"
."<td><center><img src=\"".$row['itemSmall']."\" height=\"75\" width=\"75\"  /></center></td>\n"
."<td>".$row['itemName']."</td>\n"
."<td>".$row['itemId']."</td>\n"
."<td>".$row['itemCat']."</td>\n"
."<td>".$row['itemPrice']."</td>\n"
."<td><a href=/shell/data/edit.php?id=".$row['itemId']." />Edit This Item</td>\n"
."</tr>\n";
}

echo "</table>\n";

?>[/code]

Orio.
Link to comment
Share on other sites

You have 2 while loops?
Why

your top one should be something like
if(mysql_num_rows($result) != 0){
Which means, IF the number of results from the query is NOT equal to 0 then continue.
Cos if there are no rows, then we dont want to show an empty table

The While loop
while($row = mysql_fetch_assoc($result1)){
OR
while($row = mysql_fetch_array($result1)){

basicly this gets the results from the query, and each row is placed in the car $row as a array
if u dont use a while loop, you will only get one row.

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.