Jump to content

Recommended Posts

This is the body of webpage, so far I have used the echo function to display a blank table, I have some coding stacked away for displaying the data from my database, so all I need to do is structure the coding to create 4 columns of data for each row. Bare in mind im new to php, I read from 3 different books to help me with my coding.

 

<body>
<table width="1024" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td valign="baseline" id="heading"><?php echo products ?></td>
    <td width="151" align="right" valign="bottom"><table width="150" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td height="35" align="center"><a href="../index.php" id="btn"><span>Home</span></a></td>
        </tr>
    </table></td>
  </tr>
  <tr>
    <td height="40" colspan="2" valign="middle" class="bdr"> </td>
  </tr>
</table><?php
echo "<table width='1024' border='0' align='center' cellpadding='25' cellspacing='0' class='main_bdr'>";


echo "</table>"?>
<table width="1024" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td height="40" align="center" class="bdr">© <?php echo date("Y");?> MizBones </td>
  </tr>
</table>
</body>

Sometimes a picture is worth a thousand words.  So, I dug way back in the library, and dusted off a very old function that I had.  It was tucked way back there, in amongst some crumpled paper.

<?php
//include connection info.
include('config.php'); //point to file that holds database connection info.

//function to show all results from a table.
function browseData($tbl) { //declare function, argument says to pass the function the table name. 
$result = mysql_query("SELECT * FROM `$tbl`");  //get the resource.
$output = NULL; //start the output variable.
$output .= mysql_num_rows($result) . " Results!"; //show rows returned.
$c = mysql_num_fields($result); //get a count of the fields.
$output .= "<table class=\"search\">"; //start a table.

for ($i=0; $i < $c; $i++) {  //for( start i at 0, if i is less than fields count then continue, incrementing i as it continues)
	$output .= '<th style="background-color:blue;">'.ucwords(mysql_field_name($result, $i)).'</th>';  //print a table header for each field name.
}
$d = 1; //yep, I made useless variables too, long ago. Ah, closer look it is for the background alteration of colors.
while($r = mysql_fetch_array($result)) { //fetch the array, should either be row, or assoc (in this case row).
	$output .= '<tr style="background-color:'; //new table row for each row of data.
	$output .= (($d % 2) == 0) ? '#ACCDE2;' : '#0088E2;' ; //alternate background color for each row.
	$output .= '">'; //closing the tr element.
	$i = 0; //start a new $i variable.
	while($i < $c) { //while i is less than c (field count).
		$output .= "<td>" . $r[$i] . "</td>"; //print a td element with the contents of the data row/column.
		$i++; //increment i
	}

	$output .= "</tr>"; //close row.
	$d++; //increment d (who knows).
}
$output .= "</table><br/><br/>"; //close table, adding spacing.
return $output; //return the function contents.
}

//call the function, and print the results to the page.
$db_table = 'test'; //Your table name.

echo browseData($db_table); //echoing a function will print the return of that function. (in this case $output)
?>

 

I commented this function, and even left in those things that I used to do that are not the best coding practices.  Although, I told how to fix them in the comments.

Yes I have my database connection info in a seperate file. Below is the coding I use to connect to my database, what it does is, whenever a user clicks a hyperlink for a category of products they want to see it sends a query to database and stores the data temporarily in the variable $rows.

/* Include database connection file */
include("db_con.inc");

/* Connecting to the database */
$con = mysqli_connect($host,$user,$pass,$dbase) or die("couldnt connect to server");

/* Store the variable for the category obtained from the hyperlink*/
$cat = strtolower($_GET['cat']);

/* Select the desired category from the products table */
$query = "SELECT * FROM products WHERE Cat = '$cat'";

/* Run sql query to check the table for the particular category */
$result = mysqli_query($con,$query) or trigger_error ('Could not run select query', E_USER_ERROR);

/* Store results to be used */
$rows = mysqli_fetch_assoc($result);

That last line does not do what you think it does. That's the part you'll loop through. I suggest referencing the manual, or reading the function above.

 

/* Store results to be used */

$rows = mysqli_fetch_assoc($result);

My database table has 4 columns Id, Name, Price and Pix and I need to extract the Id, Name and Picture of a particular category of product in 4 columns in each row. I need help with the structure of my code to achieve this. Below is where the structure of my code so far, which displays the products in 1 column with each product in a different row. But as I said before I want it to be displayed 4 products per row in the table.

echo "<table width='1024' border='0' align='center' cellpadding='25' cellspacing='0' class='main_bdr'>";
echo "<tr>\n
 <td width='150' align='center'><img src='{$rows['Pix']}' width='150' height='150' /></td>\n
 <td width='20' id='id'>{$rows ['Id']}</td>\n
     <td width='431' colspan='-1' rowspan='4'> </td>\n
     <td width='261' rowspan='4' align='center'> </td>\n
 </tr>\n";
}
echo "</table>";

I did some adjustment to the code below but it keeps displaying a different image 4 times in each row. What am I not doing or should be doing?

/* Create a table to hold the data */
echo "<table width='1024' border='0' align='center' cellpadding='25' cellspacing='0' class='main_bdr'>";

/*Run a loop to retrieve all rows from the database */
while ($rows = mysqli_fetch_assoc($result))
{
/* Extract all rows of data from the result */
extract($rows);

echo "<tr>";

echo "<td width='150' align='center'><img src='{$rows['Pix']}' width='150' height='150' /></td>
	  <td width='150' align='center'><img src='{$rows['Pix']}' width='150' height='150' /></td>
      <td width='150' align='center'><img src='{$rows['Pix']}' width='150' height='150' /></td>
      <td width='150' align='center'><img src='{$rows['Pix']}' width='150' height='150' /></td>";

echo "</tr>";

}
echo "</table>";

Why did you loop the same row 4 times ?

Put this piece of code immediately after extract($rows); echo '<pre>'.print_r($rows, true).'</pre>'; exit;

Post the result, please.

/* Extract all rows of data from the result */
extract($rows);
echo '<pre>'.print_r($rows, true).'</pre>'; exit;

It printed the array again plus the pictures.


Array
(
    [id] => 4907
    [Name] => Skull Candle Holder
    [Cat] => Candle Holders
    [Description] => 
    [Price] => 5.00
    [Pix] => ../images/candle_holders/4907.jpg
)






Array
(
    [id] => 5463
    [Name] => Skeletal Candle Holder
    [Cat] => Candle Holders
    [Description] => 
    [Price] => 5.00
    [Pix] => ../images/candle_holders/5463.jpg
)





Array
(
    [id] => 5879
    [Name] => Skeletal Candle Holder
    [Cat] => Candle Holders
    [Description] => 
    [Price] => 5.00
    [Pix] => ../images/candle_holders/5879.jpg
)

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.