PHP/HTML Table Not Functioning Properly - php

I'm having problems getting my PHP file to function properly. It is supposed to display a two-columned table containing departments (unordered list) and department categories (ordered list beneath each department).
The result at the moment is that the SECOND column is appearing perfectly fine, with the bulleted unordered list and numbered ordered lists beneath each bulleted department. The FIRST column, however, is being displayed WITHOUT any bullets or numbers. The data is there, but not in the format (ul and ol) that I was expecting.
Thanks for you help. Let me know if you need more information.
<?php
//displayDepartments.php
$query = "SELECT * FROM product_categories_reference
ORDER BY department_name DESC";
$category = mysql_query($query)
or die(mysql_error());
$numRecords = mysql_num_rows($category);
$catCount = 0;
$currentDepartment = "";
echo "<table><tr><td><ul>";
for ($i = 0; $i < $numRecords; $i++)
{
$row = mysql_fetch_array($category);
if ($currentDepartment != $row["department_name"])
{
if ($currentDepartment != "") echo "</ol></li>";
if ($catCount > $numRecords/2)
{
echo "</ul></td><td valign='top'><ul>";
$catCount = 0;
}
$currentDepartment = $row["department_name"];
echo "<li>$currentDepartment<ol>";
}
echo "<li><a href=\"category.php?cat='"
.$row["product_category_code"] . "'\">"
.$row["product_category_description"]
."</a></li>";
$catCount++;
}
echo "</ol></li></ul></td></tr></table>";
?>

You haven't provided css or a link, so this is a guess.
I'm assuming your css has a list-style-type set to none.
Change it to
ul {
list-style-type: disc;
}

Related

how to get the mysql columns names including the right result values in one loop

I ask in connection to this article:
How to get the columns names along with resultset in php/mysql?
$selecttable = mysql_query("SELECT * FROM $tablename WHERE (preis_bis <= '$price_upper' AND preis_bis > '$price_lower') LIMIT 1");
for ($i = 0; $i < mysql_num_fields($selecttable); $i++) {
$field_info = mysql_fetch_field($selecttable, $i);
// not WORKING
while($rowvalue = mysql_fetch_row($selecttable)) {
echo "<tr><td>Price: ".$rowvalue[$i]."</td></tr>";
}
echo "<tr><td style='border:1px solid #c0c0c0;padding:10px;'><!-- {$field_info->name} -->";
// get Logo and provider name
$select_getlogo = mysql_query("SELECT * FROM rrv_anbieter WHERE aid = '$field_info->name' LIMIT 1");
while($row_logo = mysql_fetch_object($select_getlogo)){
echo $row_logo->name.'<br><img src="images/'.$row_logo->logo.'" style="max-width: 200px;">';
}
#while($rowvalues2 = mysql_fetch_row($selecttable)) {
# foreach($rowvalues2 as $_column) {
# echo "<td>{$_column}</td>";
# }
#}
echo "</td></tr>";
}
I do not get it to get the correct row value within the "for" loop. Writing col names is working, but showing additionally the right value in same loop not.
Can someone help?
You need to iterate once more in the inner loop to get all rows of the table's columns.
while($rowvalue = mysql_fetch_row($selecttable)) {
foreach($rowvalue as $r){
echo "<tr><td> ".$r."</td></tr>";
}
}
$rowvalue[$i] showed rows incorrectly because it's following index based on the outer loop which is $i.
So basically the loop will print all rows of each column for n-times where n=number of all columns, not just the price column.
.
And you can also print all elements per-$rowvalue at once with :
while($rowvalue = mysql_fetch_row($selecttable)) {
$allElements = implode(",",$rowvalue ); // comma ',' is the separator
echo "<tr><td>". $allElements."</td></tr>";
}
}

list mysql data in 3 columns alphabetical vertical

I have this statement that works fine, but list alphabetical from left to right across 3 columns. I want it to list alphabetical vertically down the column and then continue onto the next column. Anyone know how I can do this.
$models = mysql_query("SELECT model, id FROM model where make='$phonemake' order by model asc") or die(mysql_error());
$count = 0;
$max = 3;
while($model = mysql_fetch_array( $models ))
{
$count++;
echo "<div style='float:left; width:31%; padding-right:5px;'>".$model['model']." <a href='include/delete-model.php?id=".$model['id']."' onclick='return makesure".$model['id']."();'>Delete</a></div>";
if($count >= $max){
//reset counter
$count = 0;
//end and restart
echo"<div style='clear:both;'></div>";
You have two options:
You can buffer result into array and change the order of adding of divs
The code could look like this:
$allmodels = array();
while($model = mysql_fetch_array( $models ))
{
$allmodels[] = $model;
}
for($i = 0; 3 * $i < count($allmodels); $i++)
{
for($j = 0; $j < 3; $j++)
{
if(isset($allmodels[($i * 3) + $j]))
{
$model = $allmodels[($i * 3) + $j];
// print your stuff here...
}
}
}
Or you can somehow reorder items in the sql query, maybe dump that to the temporary table or play with mysql_data_seek.
echo the data in a UL LIST , and use your counter to set the number of rows in the UL LIST , when counter is reaches max, create a new list and continue displaying. Personally i would use:
if($counter%3==0) {
create new list
}
this way you dont need to reset the counter everytime.

PHP Code is not displaying all results from databse in site but displays them in database

Have a little problem printing all the appropriate categories into a select input. Right now, it should add all categories which have children. The problem is, it's not entirely right. It only shows the very first from the main category and the subcategories that belong to it, but it is not displaying the next main category item.
For example:
Main category: Snares
Sub Category : Tama
Sub Categorys Sub Category: Starphonic
Main Category: Cymbals
Sub category: Paiste
It only displays the first main category (for eg. Snares) and everything linked to it.
I also want it to display those categories which have no sub categories AND those that have.
$result = mysql_query("SELECT COUNT(nimi) AS kpl FROM samppa_kategoriat");
$row = mysql_fetch_array($result);
$kpl = $row['kpl'];
mysql_free_result($result);
$sql = "SELECT
samppa_kategoriat.nimi AS KatNimi,
samppa_kategoriat.layer AS layerA,
samppa_kategoriat.children AS lapsetA,
samppa_alikategoriat.nimi AS AliNimi,
samppa_alikategoriat.layer AS layerB,
samppa_alikategoriat.parent AS parentA,
samppa_alikategoriat.children AS lapsetB,
samppa_alikategoriatB.nimi AS AliNimiB,
samppa_alikategoriatB.parent AS parentB,
samppa_alikategoriatB.layer AS layerC
FROM
samppa_kategoriat,
samppa_alikategoriat,
samppa_alikategoriatB
WHERE
(samppa_kategoriat.nimi = samppa_alikategoriat.parent
AND samppa_alikategoriat.nimi = samppa_alikategoriatB.parent)";
$res = mysql_query($sql);
$i = 0;
while($n = mysql_fetch_object($res)){
$countA[$i] = mysql_real_escape_string(strip_tags($n->lapsetA));
$countB[$i] = mysql_real_escape_string(strip_tags($n->lapsetB));
$nimiA[$i] = mysql_real_escape_string(strip_tags($n->KatNimi));
$nimiB[$i] = mysql_real_escape_string(strip_tags($n->AliNimi));
$nimiC[$i] = mysql_real_escape_string(strip_tags($n->AliNimiB));
$parentA[$i] = mysql_real_escape_string(strip_tags($n->parentA));
$parentB[$i] = mysql_real_escape_string(strip_tags($n->parentB));
$layerA[$i] = mysql_real_escape_string(strip_tags($n->layerA));
$layerB[$i] = mysql_real_escape_string(strip_tags($n->layerB));
$layerC[$i] = mysql_real_escape_string(strip_tags($n->layerC));
$i++;
}
for($i=1;$i<$kpl;$i++){
echo "<option>$nimiA[$i] = $countA[$i] </option>";
for($i=0;$i<$countA[$i];$i++){
echo "<option>- $nimiB[$i] = $countB[$i]</option>";
}
for($i=0;$i<$countB[$i];$i++){
echo "<option>-- $nimiC[$i]</option>";
}
}
mysql_free_result($res);
mysql_close($conn);
?>
Youre using the same variable name in your for loops.
for($i=1;$i<$kpl;$i++){
echo "<option>$nimiA[$i] = $countA[$i] </option>";
for($i=0;$i<$countA[$i];$i++){
echo "<option>- $nimiB[$i] = $countB[$i]</option>";
}
for($i=0;$i<$countB[$i];$i++){
echo "<option>-- $nimiC[$i]</option>";
}
}
When they're all using variable $i, the outer loop will not maintain its correct value.
Try to change your outer loop to $j instead of $i.

PHP while loop to get values from mysql data base if it equals the $i value

I'm trying to get a value to be inserted into a table on a webpage if the value equals $i.
$i starts at a number and decreases every loop. i can get it to work but it outputs multiple lines for each $i equivalent to the results in the table
I've reworked the code using everyones feedback to get this.
Echo "<tr><th colspan='3'><center>$rackname</th> </tr>" ;
for ($i=$RUtotal; $i > 0; $i--)
{
echo" <tr class='rackbg'><td class='i'><center>$i</td>" ;
$sql1 = "SELECT racks.rackID, racks.rackname, devices.deviceID, devices.deviceName, racks.rackRU, devices.deviceRU, devices.RUcount
FROM racks LEFT JOIN devices ON racks.rackID = devices.rackID
WHERE devices.rackID = '$rackID'";
$query1 = mysql_query($sql1);
while ($row = mysql_fetch_assoc($query1))
{
$deviceru = $row['deviceRU'];
$deviceID = $row['deviceID'];
$device = $row['deviceName'];
$deviceRUC = $row['RUcount'];
if ($deviceru == $i)
{
echo '<td class="device" rowspan='.$deviceRUC.'><a onclick=window.location="/devices.php?id='.$deviceID.'">'.$device.'</a></td><td rowspan='.$deviceRUC.'></td></tr>';
}
else
{
;
}
}
}
Echo "<tr class='rackb'><th colspan='3'>a</th></tr> " ;
This works to a degree (picture1) but when i add echo "" to the else statement it displays all wrong. (picture 2)
Any help would be greatly appreciated
Picture1 - http://imageshack.us/photo/my-images/263/examplewq.png/
Picture2 - http://imageshack.us/photo/my-images/269/example2jp.png/
I can't quite see what you're trying to do but what it looks like to me is that you want all the items from racks joined with their relevant device and displayed in order of deviceRU. Does this help:
echo "<tr><th colspan='3'><center><b>$rackname</th></tr>" ;
$sql1 = "SELECT racks.rackID, racks.rackname, devices.deviceID, devices.deviceName, racks.rackRU, devices.deviceRU, devices.RUcount
FROM racks LEFT JOIN devices ON racks.rackID = devices.rackID
WHERE racks.rackID = '$rackID' AND devices.deviceRU <= ".intval($RUtotal)."
ORDER BY devices.deviceRU;"
$query1 = mysql_query($sql1);
while ($row = mysql_fetch_array($query1))
{
$deviceru = $row['deviceRU'];
$deviceID = $row['deviceID'];
$device = $row['deviceName'];
$deviceRUC = $row['RUcount'];
echo'<tr class="rackbg"><td class="i">'.$i.'</td><td class="device">'.$device.'</td><td></td></tr>';
}
I've used a LEFT (inner) JOIN in the SQL instead of the outer join that was there before as it'll return less results and might solve your problem. I've ordered the results by deviceRU and only returned results which have deviceRU less than or equal to $RUtotal (as I think the example was showing).
I've also removed the tags, these should be replaced by using CSS to centre either all td elements or centering class="device" and class="i" e.g.:
.device, .i {
text-align: center;
}
I've also swapped your abc to abc which is the correct format for a link.
Could you describe more of the context as it's difficult to see your intention from your post.
Mat
As Peetz said, you don't need nested loop. You need something like:
$i = $RUtotal;
// ...
while ($row = mysql_fetch_array($query1)) {
// ...
if ($deviceru == $i) {
// ...
} else {
// ...
}
// ...
$i--;
}
This is looping $i times, within the outer while loop. This means you are getting the table repeated over and over again.
I suggest you remove the outer while loop.

Split records into two columns

I have a "student" table, having around 5,000 records, in my DB. I want to display those records in two divs. How do I do that without executing the query twice; only using a single query?
display example http://www.freeimagehosting.net/uploads/f1c6bb41eb.gif
Just use CSS3. Not sure how widely it is supported but saves a lot of headache and is a lot more powerful when making changes.
Use column-count, and column-width to control the number of columns and width of each column. Here's some sample code and some pretty impressive results. Prefix -webkit and -moz for now until its standardized across all browsers.
.multi-column {
/* Standard */
column-count: 2;
column-width: 150px;
/* Webkit-based */
-webkit-column-count: 2;
-webkit-column-width: 150px;
/* Gecko-based */
-moz-column-count: 2;
-moz-column-width: 150px;
}
Applied to this <div>
<div class="multi-column">
Ethelred of Wessex
Louis XII of France
George Frideric Handel
George Washington
Charles Deslandes
Andrew Jackson
Alfred Vail
William McKinley
Woodrow Wilson
Abdul-Aziz ibn Saud
Fidel Castro
Charles de Gaulle
Leonardo da Vinci
</div>
Don't you wanna see how it looks like after all this hard work?
But what if there were 3 columns? No problem.
But there's no way it can handle 4 columns you'd say:
Enough! I gotta stop adding these now
God please make it STOP!!
Just find where the "middle" is and output the end tag of the div tag and the start tag of the second div:
<?
$rowcount = mysql_num_rows($recordset);
echo "<div id='div1'>";
$i = 0;
while ($d = mysql_fetch_object($recordset)) {
echo $d->somefield;
$i++;
if ($i == floor($rowcount / 2)) {
//we have reached the mid-point, let's close the first DIV
echo "</div><div id='div2'>";
}
}
echo "</div>";
?>
get_column() function could help, it will calculate column for each item you want to show.
this sample script show how to print in two columns, but you can change it in two minutes to fix your needs if you need other number of columns.
<?php
// sample query, get members
$query = mysql_query("select name from persons");
// count total number of items to show
$total = mysql_num_rows($query);
// initiate row counter
$counter = 1;
// initiate columns contents
$column_1 = '';
$column_2 = '';
while($row = mysql_fetch_assoc($query)){
// caluculate column for current element from total items to be showed number of columns and current item
$column = get_column($total, 2, $counter);
if($column == 1){
$column_1 .= $row['name'].'<br>';
}
if($column == 2){
$column_2 .= $row['name'].'<br>';
}
$counter++;
}
// show content in two table comments
echo "<table>
<tr>
<td>$column_1</td>
<td>$column_2</td>
</tr>
</table>";
?>
and the function is:
<?php
/**
* Calculate column number where an item should be displayed on a "newspaper style"
* or "phoneguide style" report according its postion
* used to put same number of items on each column
*
* receive 3 numbers: $vp_total_size: total number of items on report
* $vp_columns : number of columns of report
* $vp_element : element position for item (1 to $vp_total_size)
*
* by Marcos A. Botta <marcos DOT botta AT gmail DOT com>
* 02/02/2007
*
*/
function get_column($vp_total_size, $vp_columns, $vp_element){
if($vp_element <= 0){
return 1;
}
if($vp_element < $vp_columns &&
$vp_columns >= $vp_total_size){
return $vp_element;
}
$vl_avg_items_by_column = $vp_total_size / $vp_columns;
$vl_items_on_first_columns = ceil($vl_avg_items_by_column);
$vl_items_on_last_columns = floor($vl_avg_items_by_column);
$vl_column_limit = ($vl_avg_items_by_column - $vl_items_on_last_columns) * $vp_columns;
$vl_allocated_items = 0;
for($i=1;$i<$vp_columns;$i++){
if($i < $vl_column_limit ||
"$i" == "$vl_column_limit"){
$vl_items_on_current_column = $vl_items_on_first_columns;
}
else{
$vl_items_on_current_column = $vl_items_on_last_columns;
}
$vl_allocated_items += $vl_items_on_current_column;
if($vp_element <= $vl_allocated_items){
return $i;
}
}
return $vp_columns;
} // get_column()
?>
good luck!
My implementation:
<?php
$students = array(1,2,3,4,5);
$split = floor(count($students)/2);
echo '<div id="parent"><div id="col-1">';
$i = 0;
foreach($students as $student)
{
echo 'Student #' . $student . '<br />';
if($i == $split)
{
echo '</div><div id="col-2">';
}
$i++;
}
echo '</div></div>';
Using the CSS3 Webkit/Moz only features are in my opinion very bad practice.
Why dont you try this code, its simple using only css, easy to understand, working in ie and mozilla...
<style type="text/css">
.ulcol
{
float: left;
width: 400px;
margin: 0;
padding: 0;
list-style: none;
}
.licol
{
float: left;
width: 200px; /*half width of the ulcol width*/
margin: 0;
padding: 0;
}
</style>
<?php
$query = mysql_query("select * from table_name") or die("Error Occured,check again");
echo '<ul class="ulcol">';
while($row = mysql_fetch_assoc($query))
{
$vartitle = $row[db_row_title];
echo '<li class="licol">';
echo $vartitle
echo '</li>';
}
echo '</ul>';
?>
Maybe split the array into two then implode them and then print both halves on each div.
I prefer to minimize any early use of "echo", because tomorrow you will want to move this in a function or a method, where "echo" should be avoided. Moreover, with the "echo" in the loop you've lost the array structure inherent to databases, and you need this structure to manipulate your data. So I would rather fill an array, process it, then implode to output.
And I would use styled bullet points to display the items, because you apparently want to display a list of items. In pseudo php code:
while row = fetch(sql)
lines[] = "<li>row_data</li>"
end
// work on the lines array, eg insert "</ul><ul>" in the middle
echo "<ul>".implode("\n",lines)."</ul>"
Try something like this
// connection goes there
$q = "SELECT `name` FROM students";
$result = mysql_query($q);
$students = array();
while($row=mysql_fetch_assoc($result)) {
$students[] = $row['name'];
}
$students_count = sizeof($students);
// chunkes into a two parts , want more columns ? just change "2" to other number
$students_chuncked = array_chunk($students,ceil($students_count/2),true);
//now display
foreach ($students_chuncked as $student_div_data){
echo '<div>',explode($student_div_data,'<br/>'),'</div>';
}

Categories