I'm trying to re-write this code so that it goes with the columns that are going to be user-defined. With this, my challenge consists of
a) Needs to select random starting item from array
b) Select the next random color from the original array that is not equivalent to the most recent items selected based the number: $intNotesColumn + 1
I was thinking a do-while statement nested inside another was appropriate for this but am unsure how to go about this. Here is my code so far:
$metroUIcolors = array( "#A30061", "#8200CC", "008987", "#A05000", "#B85A93", "#C07807", "#E51400", "#297A29" );
$metroUIcolorsLength = count($metroUIcolors);
$intNotesColumn = 3; // This will be user-defined later
// Now I query the SQL database to get my base-code
if ($result->num_rows > 0) {
// output data of each row
echo '<table border=0 valign=top>'
. '<tr>'
. '<td colspan=' . $intNotesColumn . '>' . '<h1>header</h1>' . '</td>'
. '</tr>'
. '<tr>';
$counterRank = 1;
while($row = $result->fetch_assoc()) {
echo "<td bgcolor=" . $metroUIcolors[rand(0, $metroUIcolorsLength - 1)]. ">"
. "<h2>" . $row["username"] . '</h2><br />'
. "<p class='notes'>" . $row["notes"] . "</p>"
. "<p class='footnotes'>"
. "<br />Last Reset: " . $row["lastReset"]
. '<br />Last Update: ' . $row['lastUpdate']
. '<br />SessionID: ' . $row["sessionID"]
. "<br />Counter = " . $counterRank . "</td>". '</p>';
if ($counterRank % $intNotesColumn == 0)
{
echo '</tr><tr>';
}
$counterRank++;
}
echo '</tr></table>';
} else{
echo "No Notes Found in databases";
}
Then, why don't you do it order picking one color at a time. You could use shuffle() so that there will be a different starting color everytime.
<?php
$counterRank = 1;
// shuffle the array
shuffle($metroUIcolors);
while($row = $result->fetch_assoc()) {
$rand_color = $counterRank % $metroUIcolorsLength;
echo "<td bgcolor=" . $metroUIcolors[$rand_color]. ">";
// everything else
$counterRank++;
}
?>
If you insist on doing the way you said, you may create an array $colorCount which has color codes as keys and count as values.
<?php
$counterRank = 1;
$colorCount = array_fill_keys($metroUIcolors, 0);
while($row = $result->fetch_assoc()) {
do {
$rand_color = $metroUIcolors[rand(0, $metroUIcolorsLength - 1)];
} while ($colorCount[$rand_color] > 5);
echo "<td bgcolor=" . $rand_color. ">";
// everything else
$counterRank++;
$colorCount[$rand_color] += 1;
}
?>
Related
I have a MySQL table of results of collected responses on a form that I want to output in a table.
The user rates certain variables such as Sleep from good (1) to bad (7) and each users results are on each row.
I did that successfully, but to make it more readable I want to colour code the scores based on score.
E.g., if you score 2 or below the table cell should be green, and if you score 6 or above it is coloured red.
There are 5 different variables being rated so not sure if the method I am using would work or if something more suitable.
$sql = "SELECT * FROM Responses";
$result = mysqli_query($link, $sql);
while ($row = mysqli_fetch_array($result))
{
echo '<tr>';
if($row['Sleep'] <= 2)
{
echo "<td style='background-color: green;'>" . $row['Sleep'] . "</td>";
}
elseif ($row['Sleep'] >= 6)
{
echo "<td style='background-color: red;'>" . $row['Sleep'] . "</td>";
}
else
{
echo "<td>" . $row['Sleep'] . "</td>";
}
echo '</tr>';
}
No output
Maybe so
function getColor($number)
{
if ($numner <= 2)
return 'green';
else if ($numner > 2 && $var < 6)
return 'black';
else if ($number >= 6)
return 'red';
}
$sql = "SELECT * FROM Responses";
$result = mysqli_query($link, $sql);
while ($row = mysqli_fetch_array($result))
{
echo '<tr>';
echo '<td>' . $row["Name"] . '</td>';
echo '<td style="background-color: "' . $getColor($row["Sleep"]) . '">' . $row["Sleep"] . '</td>';
}
You just put your different variables in an array
function getColor($number)
{
if ($number <= 2)
return 'green';
else if ($number > 2 && $number < 6)
return 'none';
else if ($number >= 6)
return 'red';
}
echo "<table>";
while ($row = mysqli_fetch_array($result))
{
//replace var* with your variable
$variables = array("Sleep", "var2", "var3", "var4", "var5");
echo '<tr>';
echo '<td>' . $row["Name"] . '</td>';
$i=0;
while($i<5)
{
$score=$row[$variables[$i]];
echo '<td style="background-color: ' . getColor($score). ';">' . $score . '</td>';
$i++;
}
echo '</tr>';
}
echo "</table>";
I have the following table that queries users from my database and then adds 14 players under each user. I added a couple of columns to the same database table and I am trying to get the columns to echo out and it is throwing an undefined index error, but I'm not entirely sure where I can define it in here.
This is the part I am trying to get the new columns to echo out at. I have 14 players, so this array runs through them all for each user. I am adding the same number of 'positions' So it will be player1 position1, player2 position2, etc.
for ($playerNum = 1; $playerNum <= $totalPlayerNumbers; $playerNum++) {
echo '<tr><td><div class="draftBorder">' . $playerNum . '</div></td>';
foreach ($userPlayerStore as $userPlayer) {
echo '<td><div class="draftBorder">' . $userPlayer['player' . $playerNum] . '</div></td>';
I tried to do this and this is why I am getting the errors...
foreach ($userPlayerStore as $userPlayer) {
echo '<td><div class="draftBorder">' . $userPlayer['player' . ' - ' . 'position' . $playerNum] . '</div></td>';
How else can I format this, so that I can get the position to show up next to the player. Like this:
Player1 - Position1
Full code:
<?php $userPlayerStore = array(); ?>
<table class="draft_border_table">
<tr>
<th>Rnd</th>
<?php
// Output usernames as column headings
$userResults = mysqli_query($con, 'SELECT * FROM user_players ORDER BY `id`');
while($userPlayer = mysqli_fetch_array($userResults)) {
$userPlayerStore[] = $userPlayer;
echo '<th><div>' . $userPlayer['username'] . '</div></th>';
}
?>
</tr>
<?php
// Output each user's player 1-14 in each row
$totalPlayerNumbers = 14;
for ($playerNum = 1; $playerNum <= $totalPlayerNumbers; $playerNum++) {
echo '<tr><td><div class="draftBorder">' . $playerNum . '</div></td>';
foreach ($userPlayerStore as $userPlayer) {
if (empty($userPlayer['player'.$playerNum])){
$extra_class = 'emptycell';
}else{
$extra_class = 'filledcell';
}
echo '<td class="draft_table_td">' . $userPlayer['player' . $playerNum] . '</td>';
}
echo '</tr>';
}
?>
</table>
The $userPlayer var is a reflection of each record in your database table with each index in this array being the column name. The code below should help you:
// Output each user's player 1-14 in each row
$totalPlayerNumbers = 14;
for ($playerNum = 1; $playerNum <= $totalPlayerNumbers; $playerNum++) {
// Start the player row
echo '<tr><td><div class="draftBorder">' . $playerNum . '</div></td>';
foreach ($userPlayerStore as $userPlayer) {
// Retrieve extra class name? Did you mean to use this?
$extraClass = empty($userPlayer['player' . $playerNum]) ? 'emptycell' : 'filledcell';
// Output player name and position
$playerName = $userPlayer['player' . $playerNum];
$playerPosition = $userPlayer['position' . $playerNum];
echo '<td class="draft_table_td">' . $playerName . ' - ' . $playerPosition . '</td>';
}
// End the player row
echo '</tr>';
}
How can i echo the first row of a query result 2 or more times, outside and inside of the while loop?
The following code just echo the first row outside of loop and doesn't allow to echo again inside of loop
case '1':
$db->Query("SELECT titulo_video,subtitulo_video,youtube_id,nome_norma FROM videos");
echo "<h2>" . $db->Row()->nome_norma . "</h2>";
while ($row = $db->Row()) {
echo "<h3>" . $row->titulo_video . "</h3>";
echo "<p>" . $row->subtitulo_video . "</p>";
}
break;
I want the data as follows:
<h3>row 1 - colum 1</h3>
<h2>row1 - colum 2</h2>
<p>row 1 - colum 3</p>
<h2>row2 - colum 2</h2>
<p>row 2 - colum 3</p>
<h2>row3 - colum 2</h2>
<p>row 4 - colum 3</p>
You can fiddle with the loops a bit.
if ($row = $db->Row ())
{
echo '<h2>' ...;
do
{
echo '<h3>' ...;
echo '<p>' ...;
} while ($row = $db->Row ());
}
Make use a Sentinel value:
$i = 0;
while ($row = $db->Row()) {
echo "<h3>" . $row->titulo_video . "</h3>";
echo "<p>" . $row->subtitulo_video . "</p>";
if($i==0){
echo "<h3>" . $row->titulo_video . "</h3>";
echo "<p>" . $row->subtitulo_video . "</p>";
}
$i++;
}
If you really want to treat the first row differently, handle it entirely outside of the loop, rather than including a conditional in your while loop, it's cleaner to handle it like this:
$first_row = $db->Row();
echo "<h2>" . $first_row->nome_norma . "</h2>";
echo "<h3>" . $first_row->titulo_video . "</h3>";
echo "<h3>" . $first_row->subtitulo_video . "</h3>";
while ($row = $db->Row()) {
echo "<h3>" . $row->titulo_video . "</h3>";
echo "<p>" . $row->subtitulo_video . "</p>";
}
break;
Wondering if someone could help give me a push in the right direction, I am building a search function (php and mysql) which will display search results and highlights keywords that the user has searched for. at the moment I grab the search criteria that the user has entered and query that against the database which works fine to get the desired results. the problem I have is
$highlight = preg_replace("/".$_GET['criteria']."/", "<span class='highlight'>".$_GET['criteria']."</span>", $_row['name']);
This will only highlight a phrase and not individual keywords. so for example if the document was called "Hello world" and the user typed this exactly it would highlight no problem however if the user typed "world hello" it will not highlight anything. I thought it would be a good idea to take the search criteria and use explode and check each word individually but this seems to fail as well. here is my query and how I am displaying results
$sql = "SELECT *
FROM uploaded_documents
WHERE dept_cat = 'procedures'
AND cat =:cat
AND keywords REGEXP :term ";
$result->execute(array(':cat' => $_GET['category'],':term' => $_GET['criteria']));
//display results
while($row = $stmt->fetch()){
$explode_criteria = explode(" ",$_GET['criteria']);
foreach($explode_criteria as $key){
$highlight = preg_replace("/".$key."/", "<span class='highlight'>".$key."</span>", $row['name']);
echo '<td><a target="_blank" href="'.$row['url'].'">'.$highlight.'</a></td>';
echo '<td>'.$row['version'].'</td>';
echo '<td>'.$row['cat'].'</td>';
echo '<td>'.$row['author'].'</td>';
echo '<td>'.$row['added'].'</td>';
echo '<td>'.$row['auth_dept'].'</td>';
echo '<td>';
}
}
For the sake of length I have omitted code here and tried to keep it minimal, I have been trying to base my work on the following post
highlighting search results in php/mysql
I think my first problem is the foreach loop in the while loop duplicating results but I cant think of a way around it.
Thanks in advance
In this block of code:
//display results
while ($row = $stmt->fetch())
{
$explode_criteria = explode(" ", $_GET['criteria']);
foreach ($explode_criteria as $key)
{
$highlight = preg_replace("/" . $key . "/", "<span class='highlight'>" . $key . "</span>", $row['name']);
echo '<td><a target="_blank" href="' . $row['url'] . '">' . $highlight . '</a></td>';
echo '<td>' . $row['version'] . '</td>';
echo '<td>' . $row['cat'] . '</td>';
echo '<td>' . $row['author'] . '</td>';
echo '<td>' . $row['added'] . '</td>';
echo '<td>' . $row['auth_dept'] . '</td>';
echo '<td>';
}
}
The loop is constantly referring to $row['name'], so the replacement is done, but the next time the loop happens it is replacing the next word on the original unmodified $row['name']
I think this should help you:
//display results
while ($row = $stmt->fetch())
{
$explode_criteria = explode(" ", $_GET['criteria']);
$highlight = $row['name']; // capture $row['name'] here
foreach ($explode_criteria as $key)
{
// escape the user input
$key2 = preg_quote($key, '/');
// keep affecting $highlight
$highlight = preg_replace("/" . $key2 . "/", "<span class='highlight'>" . $key . "</span>", $highlight);
echo '<td><a target="_blank" href="' . $row['url'] . '">' . $highlight . '</a></td>';
echo '<td>' . $row['version'] . '</td>';
echo '<td>' . $row['cat'] . '</td>';
echo '<td>' . $row['author'] . '</td>';
echo '<td>' . $row['added'] . '</td>';
echo '<td>' . $row['auth_dept'] . '</td>';
echo '<td>';
}
}
I've got the first round of this loop displaying correctly.
What I want is 5 rows of 8 columns. What I'm getting is the first group displays correctly and the second group displays as 10 columns.
Where am I going wrong?
echo '<table align="center" width="70%"><tr>';
$count = 0;
$rowCount = 0;
while ( $row = mysql_fetch_array($result))
{
$count++;
echo "<td><a href='" . $row['URL'] . "'><img src='" . $row['IMG'] . "' width='120' h eight='160'/></a></td>";
if ($count % 8 === 0)
{
echo '</tr>';
$rowCount++;
if($rowCount % 8 === 0)
{
echo '</tr></table><br><br>Adds here<br><br><tablealign="center" width="70%"><tr>';
}else{
echo '<tr>';
}
}
}
echo ' </tr></table>';
You're trying to make it a little too complicated.
Separate out the functionality for the column counts versus the row counts:
<?php
echo '<table align="center" width="70%"><tr>';
$count = 0;
$rowCount = 0;
while($row = mysql_fetch_array($result))
{
$count++;
echo "<td><a href='" . $row['URL'] . "'><img src='" . $row['IMG'] . "' width='120' h eight='160'/></a></td>";
if($count%8===0)
{
$rowCount++;
echo '</tr>';
if($rowCount%5===0)
{
echo '</table><br/><br/>Adds Here<br/><br/><table align="center" width="70%"><tr>';
$rowCount = 0;
}
}
}
echo ' </tr></table>';