Jump to content

Issues with table built with PHP and MySQL


jefepwnzer

Recommended Posts

Hello all,

 

  I've run into a problem with what seems to be a basic script. I want  to display two different tables of data, side by side. Each table is built with a while loop, pulling data from an MySQL query. When I try to wrap the first table in a div the result is a div that appears empty when there is really a table with 60 rows of data I expect it to wrap around. I can't get this to work, I've tried to insert the div tag in many places to no avail.

 

Also, I wanted to add some blank space at the bottom of the page so that there was room between the end of the table and the actual bottom of the page. I tried adding some <br />'s at the end of the code but that didn't work.

 

I guess I'm confused because the code is acting as if the 60 rows of table data isn't there. When I tried to add my page layout around the script (as includes at the top and bottom of the script) the layout acts as if the table isn't there, either. That is to say that my "header" and "footer" appear bunched up at the top of the page as if there wasn't content on the screen.

 

Hopefully someone can help! Thanks.

 

 

<?php

echo "<br />
          <a href='index.php'> Back Home </a>
          <br />
  <br />";
  
  
  
// -------------------------- Connect to DB


include ('connect_script.php');






// ------------------------------ Color variable for alternating table colors

$color = 1; 






// ------------------------- Query Parameters

$select = "SELECT (number) AS id, (first_name) AS fn, (last_name) AS ln, (position) AS position FROM table WHERE position = 'X' ";

$get = @mysqli_query ($dbc, $select);








// ------------------------- Run Query 

if ($get)

{

// ------------------------Start table

echo "
<div style='border-style: solid;'>

<table align='left' border='1'>

<tr>
<td>People</td>
</tr>


<tr>
<td>ID</td><td>First Name</td><td>Last Name</td><td>Position</td>
</tr>";

// ------------------------ Retrieve People

while ($row = mysqli_fetch_array($get, MYSQLI_ASSOC))


if ($color==1)

	{echo ' <tr bgcolor= #47EA7D>
			<td>' . $row['id'] . '</td><td>' . $row['fn'] . '</td><td>' . $row['ln'] . '</td><td>' . $row['position'] . '</td>
			</tr>';
			$color = '2';}


				else

					{echo ' <tr bgcolor= #A4C8B0>
			<td>' . $row['id'] . '</td><td>' . $row['fn'] . '</td><td>' . $row['ln'] . '</td><td>' . $row['position'] . '</td>
			</tr>	'; 
			$color= '1';}


// ----------------------- Close table


echo "</div>
          </table>";



mysqli_free_result ($get);


}

// --------------------   IF ERROR WITH QUERY

	else 

			{echo "Didn't connect";}



// ---------------------- Spaces --> This is the code that doesn't appear to affect the space at the bottom of the table

echo "<br />
          <br />
          <br />";


?>

Link to comment
Share on other sites

So, there were a few things missing with you code.  Mainly, the while loop didn't have {} braces, and also it was freeing the mysqli result after the first iteration due to that.

 

Here's your code cleanup and formatted in a way I prefer (I had trouble looking at your formatting, but that's just a personal thing).

<br />
          <a href='index.php'> Back Home </a>
          <br />
  <br />
<?php
// -------------------------- Connect to DB
include ('connect_script.php');
// ------------------------------ Color variable for alternating table colors
$color = 1;
// ------------------------- Query Parameters

$select = "SELECT (number) AS id, (first_name) AS fn, (last_name) AS ln, (position) AS position FROM table WHERE position = 'X' ";

$get = @mysqli_query ($dbc, $select);

// ------------------------- Run Query

if($get)
{
// ------------------------Start table

echo "
<div style='border-style: solid;'>

<table align='left' border='1'>

<tr>
<td>People</td>
</tr>


<tr>
<td>ID</td><td>First Name</td><td>Last Name</td><td>Position</td>
</tr>";

// ------------------------ Retrieve People
while ($row = mysqli_fetch_array($get, MYSQLI_ASSOC))
{


	if($color==1)
	{
		echo ' <tr bgcolor= #47EA7D>
				<td>' . $row['id'] . '</td><td>' . $row['fn'] . '</td><td>' . $row['ln'] . '</td><td>' . $row['position'] . '</td>
				</tr>';
		$color = '2';

	}else{
		echo ' <tr bgcolor= #A4C8B0>
				<td>' . $row['id'] . '</td><td>' . $row['fn'] . '</td><td>' . $row['ln'] . '</td><td>' . $row['position'] . '</td>
				</tr>	';
		$color= '1';
	}

}//end while loop
// ----------------------- Close table

echo "</div>
		  </table>";

mysqli_free_result ($get);


}//"get" (mysql query) if statement

// --------------------   IF ERROR WITH QUERY

else {
echo "Didn't connect";
}



// ---------------------- Spaces --> This is the code that doesn't appear to affect the space at the bottom of the table

echo "<br />
          <br />
          <br />";


?>

 

^^--- will this code work?  I don't know.  If an error exists after you add the while {} braces, then it's with the mysql connection/query.

 

I made a sample array and it "worked" as far as I could tell what you were trying to do.  I didn't see anything in here about a second table though.  Here's the code I tested with.

<?php
error_reporting(E_ALL);

echo "<br />
          <a href='index.php'> Back Home </a>
          <br />
  <br />";



// -------------------------- Connect to DB


//include ('connect_script.php');






// ------------------------------ Color variable for alternating table colors

$color = 1;






// ------------------------- Query Parameters

//$select = "SELECT (number) AS id, (first_name) AS fn, (last_name) AS ln, (position) AS position FROM table WHERE position = 'X' ";

//$get = @mysqli_query ($dbc, $select);



$row = array(
"id" => range(0,25),
"fn" => range("A","Z"),
"ln" => range("z","a"),
"position" => range(50,75)
);


$get = true;

// ------------------------- Run Query

if ($get)

{

// ------------------------Start table

echo "
<div style='border-style: solid;'>

<table align='left' border='1'>

<tr>
<td>People</td>
</tr>


<tr>
<td>ID</td><td>First Name</td><td>Last Name</td><td>Position</td>
</tr>";

// ------------------------ Retrieve People

$counter = 0;
while ($counter < 26) {//$row = mysqli_fetch_array($get, MYSQLI_ASSOC))


if ($color==1)

	{echo ' <tr bgcolor= #47EA7D>
			<td>' . $row['id'][$counter] . '</td><td>' . $row['fn'][$counter] . '</td><td>' . $row['ln'][$counter] . '</td><td>' . $row['position'][$counter] . '</td>
			</tr>';
			$color = '2';}


				else

					{echo ' <tr bgcolor= #A4C8B0>
			<td>' . $row['id'][$counter] . '</td><td>' . $row['fn'][$counter] . '</td><td>' . $row['ln'][$counter] . '</td><td>' . $row['position'][$counter] . '</td>
			</tr>	';
			$color= '1';}


// ----------------------- Close table

$counter++;
}
echo "</div>
          </table>";



//mysqli_free_result ($get);


}

// --------------------   IF ERROR WITH QUERY

	else

			{echo "Didn't connect";}



// ---------------------- Spaces --> This is the code that doesn't appear to affect the space at the bottom of the table

echo "<br />
          <br />
          <br />";


?>

Link to comment
Share on other sites

My fault for not looking properly, change the following:

 

	echo "</div>
                          </table>";

To...

echo "</table><div style='clear:both;'></div></div>";

 

Try that? Tell me if it works, I got it working for me and it still aligns left.

 

Regards, PaulRyan,

Link to comment
Share on other sites

Drummin: That worked, the div now wraps around the table and I can position it as needed, thank you very much!

 

PaulRyan: I added in your code, it didn't seem to have any affect on the table after I added in Drummin's code, not sure if any affect should be seen.

 

I still have one problem remaining: I can't get anything to align below the table. "This text should be at the bottom" has been placed at the end of the code and yet it still appears at the top (see image below). Is this proper and I'm just not understanding something or should it be appearing at the bottom? Right now the code is

 

echo "<br />
                    This text should be at the bottom";

 

but I've also tried simply putting it after the PHP code, in HTML, but I get the same result. Like I said before, I'm trying to add some space in between the end of the table and the page, and eventually there will be a footer that needs to sit below the table.

 

<html>

<head>
</head>

<body>


<?php

// PHP code for the table

?>

This text should be at the bottom

</body>

</html>

 

Thanks again for everyone's help and quick replies, I really appreciate it.

 

 

error2.jpg

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.