Trying to build a html table, which will be displaying the buildings a player is allowed build in a game. And there should also be a column that displays how many the player already own.
The code to display the table works as well as all the buildings you can build, but I can't get it to display how many of what building you already own.
Code is a bit messy now with some out commented queries, but I kept them in there to show what I have already tried to get them to display correctly.
EDIT(With C Würtz code):
// Fetch buildings
$result = $database->query("SELECT id, name, description, cost, power_use FROM buildings;");
$buildings = array();
while($building = $database->fetch($result)) {
$buildings[$building['id']] = $building;
}
// Buildings Owned
$buildings_owned = $database->query("SELECT building_id, count(*) as n FROM player_buildings WHERE owner_id = '$user_id'");
$buildings = array();
while($owned = $database->fetch($buildings_owned)) {
$owned_buildings[$owned['id']] = $owned;
}
$playerBuildings = array();
while($owned = $database->fetch($buildings_owned)) {
$playerBuildings[$owned['buildings_id']] = $owned['n'];
}
// Display form
echo "For every 10 building you construct, it will also cost you 1 turn!";
echo "<table style='width:900px;'>
<tr>
<th style='border: solid black 1px;width:40% text-align:left;'>Name</th>
<th style='border: solid black 1px;width:50%;'>Description</th>
<th style='border: solid black 1px;width:5%;'>Price</th>
<th style='border: solid black 1px;width:5%;'>Power Usage</th>
<th style='border: solid black 1px; width:5%;'>Buildins Owned</th>
<th style='width:5%;'> </th>
</tr>";
foreach ($buildings as $building) {
$bid = $building['id'];
$building['player_count'] = isset($playerBuildings[$bid])
? $playerBuildings[$bid]
: 0;
echo "<tr>
<td style='border: solid black 1px;'>{$building['name']}</td>
<td style='border: solid black 1px;'>{$building['description']}</td>
<td style='border: solid black 1px;'>{$building['cost']}</td>
<td style='border: solid black 1px;'>{$building['power_use']}</td>
<td style='border: solid black 1px;'>{$owned['amount']} </td>
<td>
<form action='$self_link' method='POST'>
<input type='hidden' name='building_id' value='$id' />
<input style='width:40px' type='number' name='amount' value='amount' />
<input type='submit' name='build' value='Build' />
</form>
</td>
</tr>";
}
echo "</table>";
The first query is like SELECT id, name, etc FROM buildings; to PHP array $buildings.
The second could be like SELECT buildings_id, count(*) as n FROM player_buildings WHERE owner_id = :user_id;. Then order the PHP array:
$playerBuildings = [];
while($owned = $database->fetch($buildings_owned)) {
$playerBuildings[$owned['buildings_id']] = $owned['n'];
}
In the view, just loop on $buildings and check if there is a relative $playerBuildings.
foreach ($buildings as $building) {
$bid = $building['id'];
$building['player_count'] = isset($playerBuildings[$bid])
? $playerBuildings[$bid]
: 0;
// render something
}
SELECT count(*) as owned_buildings FROM player_buildings WHERE owner_id = '$user_id'
Related
I'm trying to process a pretty simple rental quote form, basically the form gets filled out and then the data gets sent to an email.
My issue is how the item section is being formatted. Right now I'm trying a table and populating it with a foreach loop. The main problem is it's generating empty td tags for some reason.
Here's the output, I've added some borders so you can see the empty cells being generated:
And here's the section of my php code where I'm processing this (if you'd like to see the entire code, just let me know):
<?php
if(isset($_POST['submit']))
{
$name = strip_tags($_POST['renter_name']);
$email = strip_tags($_POST['renter_email']);
$message = strip_tags($_POST['renter_message']);
$subject = "Quote Request";
$rentalItems = ($_POST['item']);
// $items = implode(',', $_POST['item']);
$msg = "<html><body style='font-family:Arial,sans-serif;'>";
$msg .= "<p><strong>Sent by:</strong> ".$name."</p>";
$msg .= "<p><strong>Items:</strong></p>";
$msg .="<table name='rental_items' style='border-collapse:collapse; border: 1px solid black;';>
<thead style='text-align:left;'>
<tr>
<th sytle='padding-right: 5px;'>Item Name and ID</th>
<th>Quantity</th>
</tr>
</thead>
<tbody>";
foreach($rentalItems as $item) {
$msg .="<tr>
<td style='border: 1px solid black;'>". $item['name'] ."</td>
<td style='border: 1px solid black;'>".$item['quantity']."</td>
</tr>";
}
$msg .= "</tbody>
</table>";
$msg .= "<p><strong>Message:</strong> ".$message."</p>";
$msg .= "</body></html>";
Currently I'm running a row and two cells in the loop. But for some reason it's treating each td as its own row.
Ultimately I would like that Quantity number along side the Name and ID info.
I'll be honest with you, I haven't worked with tables in years so it could possibly be something super simple I'm missing or have wrong. Or it could be how I'm structuring this whole thing in general.
Any advice or help would be greatly appreciated. Again, if you need any more info, just let me know.
Here's the item section of the form:
<input type="checkbox" name="item[][name]" value="Name: <?php the_title();?> <?php echo "<br>"?> ID: <?php echo get_post_meta($post->ID, 'id_number', true); ?>" id="<?php the_title(); ?>"/>
<label for="<?php the_title(); ?>"><img src="<?php the_post_thumbnail_url(); ?>" alt="<?php the_title(); ?>" class="form-image" /></label>
<input type="text" name="item[][quantity]"/>
As you are using item[][name] and item[][quantity] for the data, you will end up with alternate array elements containing the different values...
Array(0 => ("name" =>"somename"),
1 => ("quantity" =>1))
When you foreach over this array, it will display one value or the other, but not the 2 together.
So it would be easier (I think) to use a for loop and take it in steps of 2...
for($i = 0; $i < count($rentalItems); $i+=2) {
$msg .="<tr>
<td style='border: 1px solid black;'>". $rentalItems[$i]['name'] ."</td>
<td style='border: 1px solid black;'>".$rentalItems[$i+1]['quantity']."</td>
</tr>";
}
Alternatively - rename the arrays to be item[name][] and item[quantity][] so that the data is separate, but you can then use $item['name'][0] and $item['quantity'][0].
I want to search and execute the data with the same orderid . Unfortunately it says
Fatal error: Cannot use object of type mysqli_result as array in
C:\wamp64\www\Workshop1\admin\ordersearch.php on line 114
if(isset($_POST['search']))
{
// id to search
$Orderid = $_POST['Orderid'];
// connect to mysql
$connect = mysqli_connect("localhost", "root", "","workshop1");
// mysql search query
$query = "SELECT * FROM orders WHERE Orderid = $Orderid ;";
$result = mysqli_query($connect, $query);
// if id exist
// show data in inputs
if(mysqli_num_rows($result) > 0)
{
while ($row = mysqli_fetch_array($result))
{?>
<td style='border-bottom: 1px solid #b3b3b3;' align='center'><?php echo $result["Orderid"];?></td>
<td style='border-bottom: 1px solid #b3b3b3;' align='center'><?php echo $result["productid"];?></td>
<td style='border-bottom: 1px solid #b3b3b3;' align='center'><?php echo $result["price"];?></td>
<td style='border-bottom: 1px solid #b3b3b3;' align='center'><?php echo $result["quantity"];?></td>
<td style='border-bottom: 1px solid #b3b3b3;' align='center'><?php echo $result["custid"];?></td>
<?php
}
}
// if the id not exist
// show a message and clear inputs
else {
echo "Undifined ID";
}
mysqli_free_result($result);
mysqli_close($connect);
}
you must write your code like this because if you don't it will recognize $Orderid as
a string.
$query = "SELECT * FROM orders WHERE Orderid = '" . $Orderid. "' limit 1 ";
and change this
<td style='border-bottom: 1px solid #b3b3b3;' align='center'>
<?php echo $row["Orderid"];?>
</td>
You are store data in loop using $row variable so that you need to use $row['orderId']; if you print data in loop for ex. print_r($row);
I want a zebra striped table.
The data is extracted from the database and every instance creates a new table.
I want the zebra-striped on the <table> level. this would mean that every other <table> element gets a different color.
I tried to add a class to my <table class="oddeven"> but this still does the changing on every tr.
Here is the code I use it on:
<?php
global $wpdb;
$sql = "SELECT * FROM $wpdb->postmeta WHERE meta_key = 'group' AND meta_value='$group'";
$results = $wpdb->get_results($sql) or die(mysql_error());
echo'<table cellpadding="0" cellspacing="0" border="0" width="100%">';
foreach( $results as $result )
{
$post_id = $result->post_id;
$company_name = get_post_meta($post_id, 'company_name', true);
$address = get_post_meta($post_id, 'address', true);
$postal_code = get_post_meta($post_id, 'postal_code', true);
$city = get_post_meta($post_id, 'city', true);
$phone = get_post_meta($post_id, 'phone', true);
echo '
<tr>
<td>
<table cellpadding="0" cellspacing="0" border="0" width="100%" class="oddeven">
<tr>
<td width="75%">
<strong>'.$company_name.'</strong>
</td>
<td rowspan="4"><img class="table_image" src="'.get_bloginfo('template_directory').'/images/arrow.png"></td>
</tr>
<tr>
<td>
'.$address.'
</td>
</tr>
<tr>
<td>
'.$postal_code.' '.$city.'
</td>
</tr>
<tr>
<td>
'.$phone.'
</td>
</tr>
</table>
</td>
</tr>';
}
echo '</table>';
?>
This is the styling:
(while typing this I realise that this is on tr:level)
.oddeven tr:nth-child(odd){
background: #ffffff;
}
.oddeven tr:nth-child(even){
background: #000000;
}
I hope I make myself clear
If you change your foreach loop to a for loop like this:
for($i = 0; $i < count($results); $i++) {
$result = $results[$i];
...
Then you can figure out if the current row is even or odd by testing if $i % 2 == 0. If that evaluates to true, then add an 'even' class; else add an 'odd' class.
If you do not want to propagate the change to child tables, you can also enforce the same color on the children :
.top tr:nth-child(odd) {
background-color: red;
}
.top tr:nth-child(odd) tr {
background-color: red;
}
.top tr:nth-child(even) {
background-color: yellow;
}
.top tr:nth-child(even) tr {
background-color: yellow;
}
I assume that's what your want since you already have stripped rows on your nested tables
Or maybe I got it wrong, you might have to explain what the <table> level is, since your have nested tables.
I am trying to create a class record. I created a table, with column of names and activities.
the table looks like this.
Name | Q1 | Q2 |
--------------------------------
Prinz | 20 | (input text)
My aim is to save the input data into my database, but I need to have the id of the student(Prinz). I already managed to save the grade but want the id of that particular student to be save as well. My MYSQL table has 4 att. namely, grade_id(auto_inc), stud_id, grade and the activity type(Quiz, Assignment, Exam).
Here's my code
<table border="0" cellspacing="0" cellpadding="0" style="width: 845px; margin-top: 20px; margin-bottom: 65px;">
<tr style="background-color: #caccca; text-align: center;">
<td style="width: 130px;" class="td-1">NAME</td>
<?php
$result = mysql_query("select * from subject_grade ");
$numrows = mysql_num_rows($result);
while ($row = mysql_fetch_object($result)) {
$act_type = $row->act_type;
echo '<td style="width: 130px;" class="td-1">'.$row->act_type .'</td>';
}
?>
<td class="td-1">GPA</td>
</tr>
<?php
$result = mysql_query("select * from student_db ");
while ($row = mysql_fetch_object($result)) {
echo '<tr class="border" style="background-color: #f9f9f9;"><td style="width: 150px;" class="td-2" name="student"><p>'.$row->firstname .' '. $row -> lastname .'</p></td>';
$id = $row->student_id;
for ($i=0; $i<$numrows; $i++){
echo '<form action ="record.php" method="POST"><td style="text-align: center;"><input type="text "style="font-size: 12px; width: 20px;" name="grade"><input style="display: none;" type="text" name="id" value="'.$id.'"><input type="submit"></td></form>';
}
echo '<td style="width: 80px;"class="td-2"></td>';
}
?>
</tr>
</table>
RECORD.PHP File
if($_POST['grade']){
$sql="INSERT INTO class_record (grade, student_id)
VALUES
('$_POST[grade]','$_POST[id]')";
$rs1 = mysqli_query($con,$sql);
echo $_POST['grade'] .' '. $studID;
} else echo 'ERROR';
?>
I hope you can help me, this is for our school project.
What data type are you storing student_id as?
('$_POST[grade]','$_POST[id]')";
use this query
$sql="INSERT INTO class_record (grade, student_id)
VALUES
('".$_POST['grade']."','".$_POST['id']."')";
I'm getting a table appearing after the first result of the nested while loop.
What I'm trying to do is display the usernames as a list by selecting from the "users" table, then comparing that list with the project engineers from the "current projects" table.
This way there is a list of project engineers with their assigned projects underneath their names.
Unfortunately, I'm after the first nested while loop runs, it spits out a empty table and I'm not sure how to get rid of it.
I'm suspecting it has to do with the initial variable of $proj_engineer = "";
Does anyone know where this empty table is coming from and how to get rid of it?
Below is the code:
<?php
$query = mysql_query("SELECT * FROM `users` ORDER BY `username` ASC") or die(mysql_error());
while ($row = mysql_fetch_assoc($query)) {
$user_id = $row['user_id'];
$username = $row['username'];
$proj_engineer = "";
$query1 = mysql_query("SELECT * FROM `current_projects` WHERE `proj_engineer`='$username' ORDER BY `proj_engineer` ASC") or die(mysql_error());
while ($row1 = mysql_fetch_assoc($query1)) {
$proj_id = $row1['proj_id'];
$proj_engineer = $row1['proj_engineer'];
}
echo "<table border=1><tr><td colspan=12><p class='bold18'>" . $proj_engineer . "</p></td></tr>";
$query3 = mysql_query("SELECT * FROM `current_projects` WHERE `proj_engineer`='$username' ORDER BY `proj_id` DESC") or die(mysql_error());
while ($row3 = mysql_fetch_assoc($query3)) {
$proj_id = $row3['proj_id'];
$proj_number = $row3['proj_number'];
$proj_name = $row3['proj_name'];
$proj_sort = $row3['proj_sort'];
$proj_start = $row3['proj_start'];
$proj_finish = $row3['proj_finish'];
$proj_overstat = $row3['proj_overstat'];
$proj_dwgstat = $row3['proj_dwgstat'];
$proj_soostat = $row3['proj_soostat'];
$proj_substat = $row3['proj_substat'];
$proj_engineer = $row3['proj_engineer'];
$proj_drafter = $row3['proj_drafter'];
$proj_rating = $row3['proj_rating'];
$proj_pending = $row3['proj_pending'];
$proj_notes = $row3['proj_notes'];
$start_time = date("m/d/y", $proj_start);
$finish_time = date("m/d/y", $proj_finish);
echo "
<tr>
<td width=40>$proj_number</td>
<td width=100>$proj_engineer</td>
<td width=100><a href='./project-page.php?proj_id=$proj_id'>$proj_name</a></td>
<td width=40>$start_time</td>
<td width=40>$finish_time</td>
<td width=110>
<div style='position:relative; background:url(images/bar01.gif); width:$proj_overstat; height:20;'>
<div style='position:absolute; bottom:0; left:0; width:$proj_overstat; font-weight:bold; color:#000000; vertical-align:middle; height:20; text-align:center;'>$proj_overstat%</div>
</div>
</td>
<td width=110>
<div style='position:relative; background:url(images/bar02.gif); width:$proj_dwgstat; height:20;'>
<div style='position:absolute; bottom:0; left:0; width:$proj_dwgstat; font-weight:bold; color:#000000; vertical-align:middle; height:20; text-align:center;'>$proj_dwgstat%</div>
</div>
</td>
<td width=110>
<div style='position:relative; background:url(images/bar03.gif); width:$proj_soostat; height:20;'>
<div style='position:absolute; bottom:0; left:0; width:$proj_soostat; font-weight:bold; color:#000000; vertical-align:middle; height:20; text-align:center;'>$proj_soostat%</div>
</div>
</td>
<td width=110>
<div style='position:relative; background:url(images/bar04.gif); width:$proj_substat; height:20;'>
<div style='position:absolute; bottom:0; left:0; width:$proj_substat; font-weight:bold; color:#000000; vertical-align:middle; height:20; text-align:center;'>$proj_substat%</div>
</div>
</td>
<td width=40 align='center'><a href='project-notes.php?proj_id=$proj_id'><img src='images/note.png' border=0></a></td>
<td width=40 align='center'><a href='./project-edit.php?proj_id=$proj_id'>EDIT</a></td>
<td width=40 align='center'><a href='./project-delete.php?proj_id=$proj_id'>DELETE</a></td>
</tr>
";
}
echo "</table><br>";
}
?>
This might be because, your query: -
$query1 = mysql_query("SELECT * FROM `current_projects` WHERE
`proj_engineer`='$username' ORDER BY `proj_engineer` ASC")
or die(mysql_error());
fetched you an empty value for $proj_engineer. You can do a check before printing your table, whether your variable contains a value instead. Try printing the $proj_engineer before printing the table.
You can enclose your code echoing the table inside an if construct, which will print table only when your value is not empty.