Create unordered list from column in mysql database - php

I have two tables.
Table A lists playerID(PK), Name, etc.
Table B lists awardID(PK), playerID(FK), playerOfWeek, allStar, etc.
As some players can have more than one award (such as been awarded player of the week twice -- they would have a different awardID #), how could I create an ordered list to display the value in the appropriate player award column?
I'm first using a function call
function create_unordered_list($listitems) {
echo "<ul>";
foreach($listitems as $value) {
echo "<li>".$value."</li>";
}
echo "</ul>";
}
I'm storing the result of the column in:
$All_Star = $row['award_AllStar'];
Then, I'm trying to echo out the list:
echo create_unordered_list ($All_Star);
Any help, very much appreciated.
Thanks,
Ken

Try to print_r($listitems) inside the function and show us what it is printing.

Related

How to enable onclick type events with the output of a mysqli_fetch_array($result))

I have been trying to get my head around how to approach this problem. I am writing a web page book library with the categories ('nodes') as MySQL records. I want to print the list of categories at each level, starting at the highest level, and then allow the user to select a category to travel deeper into the library. The PHP codes runs a saved procedure in MySQL:
//loop the result set
while ($row = mysqli_fetch_array($result)) {
if ($row[1] <> 0) {
echo $row[0];
echo "<br />";
} else { // the first row is just the heading
"</strong>Category: ";
echo $row[0];
echo " : <br />";
}}
Because there are only two test categories, this produces output:
Books :
Nature
Children's Books
However, I want to be able to create an onclick event over 'Nature' and 'Children's Books' so the user can select a category and drill down t the next level via a php function. I can convert the php output into html eg:
<?= "<p>{$row[0]}</p>" ?>
but I can't see how I can identify the row in an onclick event to pass a parameter to the function. Perhaps I need to have a completely different approach?
Add an onclick attribute to the element that calls a JavaScript function that does what you want.
<?= "<p onclick='someFunc({$row['id']})'>{$row[0]}</p>" ?>
Replace id with the actual name of the column containing the ID of the row in the table. someFunc() can use that ID to look up information in an array or object, or send an AJAX request.

Declare order to show foreach output with PHP

I am currently trying to set up a custom sortable list. Without getting into too much detail as this would become a very large post, I would like to use the PHP foreach function to show the list.
I currently have
$video_id = $row['videos'];
$res = preg_replace("/[^0-9,.]/", "",$video_id);
$exclude = explode(',', $res);
} ?>
....
<ul id="sortable2" class="connectedSortable">
<?php foreach ($video_list as $key => $item): ?>
<?php if(in_array($item['id'], $exclude)){ ?>
<li id="video-<?php echo $item['id'] ?>"><?php echo $item['id'],
$item['title'] ?></li>
<?php } ?>
<?php endforeach; ?>
</ul>
This generates a list of items from the database excluding items with an ID from an array which is also fetched from another table.
In this example, it will only show videos with the id 3,4 and 2. This is all working as expected. My issue is once I have reordered the items and refreshed the page they are loaded in numerical order. I need these to be load in the order specified (3,4 and 2 for this example).
Any help would be highly appreciated.
Thanks
I think what you are looking for is ORDER BY FIELD (MySql), this lets you order by a field in a specific way.
I need these to be load in the order specified (3,4 and 2 for this example).
Sounds like a good fit to me!
For example
SELECT * FROM table WHERE id IN(3,4,2) ORDER BY FIELD(id, 3,4,2)
This will select rows from table where the id is in the list and then order the results by id in the order they are place there, 3 then 4 then 2.
excluding items with an ID
This might cause some issue, the Field order by assumes you know at least some of the values in the field. Excluding implies that you know what you don't want but not necessarily what you do want, if that makes sense.
Maybe it's not an issue, I don't have enough information on your particular use case to determine that, so I am just mentioning it as a cautionary thing.
Thanks!

PHP: Split results of query into 2 columns

I want to load contacts from a query, Whilst I have found ways to split by the amount of rows I want the result to be split into two columns as below as well as them being a sort of clickable button.Is it possible to display the query results as described below by the use of HTML or by the query itself? :
Contacts
------------------------------
Albert Smith | Ben Marshall
Benjamin Jones | Chris Jones
I have tried the code below
<?
$rowcount = mysql_num_rows($records);
echo "<div id='column1'>";
$i = 0;
while ($d = mysql_fetch_object($records)) {
echo $d->name;
$i++;
if ($i == floor($rowcount / 2)) {
echo "</div><div id='colums2'>";
}
}
echo "</div>";
?>
But this splits the columns by reaching half of the results in 1 column and the rest in another column
Albert Smith | Benjamin Jones
Ben Marshall | Chris Jones
This is not possible by query itself.
You must parse the result with an html <table>, every two results make a new <tr> and double <td>
I'm guessing you are conversant with coding:
Check this algorithm
get results
declare an even/odd check variable
loop through your result-set
List item
increment the counter in each loop
ii. check counter value in the loop
iii. if counter variable value is even give it a different class
To make them a clickable button, you can use bootstrap as (ie. give an tag the style of a button)
Let me know on how it works out.
Have fun coding.

Should I create another table column or is there another way I can go about doing this?

I'm having an issue trying to figure out how to solve this problem.
I have a database with a Table that holds job information. There's a column called "Payments" and the values % per payment term are split up here like so: http://i.imgur.com/Y2Lb48e.png
So at the bottom the values will read 40%, 40%, 20%.
In the form, these values are displayed exactly the same as in the database, 40/40/20.
I was tasked with creating 4 option boxes instead with values in each ranging from 10% to 100%. Here's what the code looks like.
<label for='payment'>Payment Terms</label>
<?php
// Create 4 % boxes that add up to %100 for Payment Terms
// Must validate and add up to %100
$selects = 4;
if (isset($data2['payment'])) {
$percentages = explode("/", $data2['payment']);
// Make sure that the array is long enough by adding zeroes to the end
while (count($percentages) < $selects) $percentages[] = 0;
}
else {
$percentages = array_fill(0, $selects, 0);
}
$options = range(10, 100, 10);
for ($i = 0; $i < $selects; ++$i) { ?>
<ul style="list-style-type: none;"><li><select id="paymentSelect">
<option></option>
<?php foreach ($options as $o) { ?>
<option value="<?php echo $i; ?>"
<?php if ($o == $percentages[$i]) echo ' selected="selected"'; ?>>
<?php echo $o; ?>
</option>
<?php } ?>
</select></li></ul>
<?php } ?>
This is what the form looks like now: http://i.imgur.com/2OvDtsn.png
The blue date boxes is what I want to create. Will I have to make a new column in my table? How will I link each percentage to each date since the percentages are already in the table?
You could make a new table in your data base "Payments" and have a foreign key to tie it back to the original table, your payment column and your date column. Then you can have as many payment percents and Dates you would ever want.
I might be tempted to future proof this for 10 options or 20 options or whatever to save always changing the columns in the table. How about you have 4 rows to show your payment terms - pairs for each date and %. If you have multiple possibilities - i.e. several clients with different payment terms then your table should have a client_id. This way you can have some clients with 2 payments, others with 50 - simply by adding rows with fields (client_id, %, date).
Validation is the same and the table much simpler. To retrieve the payment data you are fetching multiple rows that make table building from PHP fairly straightforward too.
Best
Nick

php create html table from two arrays and mysql query producing extra cells

I am trying to create an html table using php and mysql to track the number of items which have reached certain stages in a multi-stage process. I need to display the names of the stages as a slanted header row (already sorted that with css3, blank column as first) and each row will have the name of the type of item in the first cell, followed by the count of items at each stage in the appropriate columns.
The stages have a numerical value so a simple array is fine - but there are 16 possible stages and this has to be query-driven as different people will see different stages.
The types of item vary according to another relationship (anything from one to 614 types at this time) but I have a query that tells me the distinct name to display on each row and a code for the type of item.
My code produces a horizontal header with the sixteen stages (plus an extra cell at the start to allow for the vertical legend)
The rows of data start with the legend cell but somehow the loops are then giving me 32 cells per row (help please). The results I expect are in the 1st, 3rd etc. There is no code that should deliver an extra blank as I put a zero for no match. So here it is:
// a previous $sql query checks for the distinct types of item in the batch in the table with joins to two other tables that accord with the two components of the CategoryCode
$today=date('H:i:s d-m-Y');
$categs=array();
while($row=mysql_fetch_assoc($result)){
$categs[$row['CategoryCode']]=$row['description'];
}
$headrow='';// sets variable for the header row
ed to
$bodyrow='';// sets variable for the body rows
$sql="SELECT stage_id, short_stage_desc FROM process_stages WHERE stage_omit='0' ORDER By showord"; // for various historic reasons the stages do not follow a 1,2,3 pathway so this query needs a showord column to produce some logic
$result = mysql_query($sql);
$cou=mysql_num_rows($result);
$stages=array(); // sets up array to check against
$s='1';
while($row=mysql_fetch_assoc($result)){
$s++; $ccolor = ($s % 2) ? "odd" : "even"; //for css background cell colour and the mix of div and spans that follow are part of doing 45 degree slanted column headings
$stageid=$row['stage_id']; $stagedesc=$row['short_stage_desc'];
$headrow.="<th class='skew'><div class='$ccolor'><span>$stagedesc</span></div></th>";
$stages[]=$row['stage_id']; //puts stages in display order
}
$headrow.="</tr></thead><tbody>";
//Now for the table bodyy
foreach($categs AS $key=>$category_descript){
$bodyrow.="<tr><td class='item-cat'>$category_descript</td>";//produces first cell as label
$sql="SELECT process_stage, COUNT(DISTINCT RowID) AS itemcount FROM item_detail WHERE CategoryCode='$key' GROUP BY process_stage";
$result=mysql_query($sql);
$gothro=array(); //will hold the key for the stage and number of items at that stage
while($row=mysql_fetch_assoc($result)){
$stag=$row['process_stage'];
$gothro[$stag]=$row['itemcount']; // fills array
}
$s='1';
reset($stages); // just ensure the array pointer is at start
foreach($stages AS $stagval){
$s++; $ccolor = ($s % 2) ? "odd" : "even"; // css for body row
if(array_key_exists($stagval,$gothro)){$bodyrow.="<td class='$ccolor'>".$gothro[$stagval]."<td>";}else{$bodyrow.="<td class='$ccolor'>0<td>";}
}
$bodyrow.='</tr>';
}
$outs="<br /><br /><div class='item-table-container'>
<h4>$bnam Situation at $today</h4>
<div class='item-table'>
<table><caption>$batch_description</caption><thead><tr><th>Item Type</th>$headrow.$bodyrow<tbody></table> </div></div> ";
Looks like you missed a forward slash in a closing td tag here:
... else{$bodyrow.="<td class='$ccolor'>0<td>";}

Categories