So I am trying to do this..
$userID = $_SESSION['user_session'];
$stmt = $this->db->prepare("SELECT * FROM orrs");
$stmt->bindparam(":id", $userID);
$stmt->execute();
$count = $stmt->rowCount();
echo
"<div class='table-responsive'>
<table class='table' border='1'>
<tr class='head'>
<h3>Snapshot</h3>
<th>Se</th>
<th>#s</th>
<th>Ae</th>
<th>Prt</th>
<th>Pin</th>
</tr>";
while($userRows=$stmt->fetch(PDO::FETCH_ASSOC)) {
if($userRows['stage'] == '1')
{
echo "<tr>";
echo "<td>" . "Newn" . "</td>";
echo "<td>" . $count . "</td>";
echo "<td>" . $userRows['aow'] . "</td>";
echo "<td>" . $userRows['pit'] . "</td>";
echo "<td>" . $userRows['pgin'] . "</td>";
}
else if($userRows['stage'] == '2')
{
echo "<tr>";
echo "<td>" . "Pendinn" . "</td>";
echo "<td>" . $count . "</td>";
echo "<td>" . $userRows['gfh'] . "</td>";
echo "<td>" . $userRows['pt'] . "</td>";
echo "<td>" . $userRows[trin'] . "</td>";
}
}
Basically, If the value in the row STAGE = 1 I want it to count those rows and give me the number.. If the value of STAGE = 2 I want it to count those rows and give me the number.
Right now, It is just counting all of the rows.. So for both of the IF statment its count number is saying 2, When there is only 1 in each section..
I think you need to execute three different statements, one to get all the rows (for you to loop over and create your output) and one to get each of the counts
//The current one
$stmt = $this->db->prepare("SELECT * FROM orrs");
//The get the count for stage '1'
$stage_1_stmt = $this->db->prepare("SELECT * FROM orrs where STAGE = 1");
$stage_1_count = $stage_1_stmt->rowCount();
//The get the count for stage '2'
$stage_2_stmt = $this->db->prepare("SELECT * FROM orrs where STAGE = 2");
$stage_2_count = $stage_2_stmt->rowCount();
You can execute these others to get the counts which you should use in place of $count in your loop.
Your while loop then becomes
while($userRows=$stmt->fetch(PDO::FETCH_ASSOC)) {
if($userRows['stage'] == '1')
{
echo "<tr>";
echo "<td>" . "Newn" . "</td>";
echo "<td>" . $stage_1_count . "</td>";
echo "<td>" . $userRows['aow'] . "</td>";
echo "<td>" . $userRows['pit'] . "</td>";
echo "<td>" . $userRows['pgin'] . "</td>";
}
else if($userRows['stage'] == '2')
{
echo "<tr>";
echo "<td>" . "Pendinn" . "</td>";
echo "<td>" . $stage_2_count . "</td>";
echo "<td>" . $userRows['gfh'] . "</td>";
echo "<td>" . $userRows['pt'] . "</td>";
echo "<td>" . $userRows[trin'] . "</td>";
}
}
Related
I am using the below php code to list Alphabetical(A-Z) characters in the rows. It works fine from a to z. My problem is that after "z", it starts with aa, ab,ac, etc.. I'd like it to start with a to z again every time it comes to z.
How can achieve this? thanks.
<?php
foreach (range('a', 'z') as $char)
while($row = mysqli_fetch_array($search_result))
{
echo "<tr>";
echo "<td>" .$char. "</td>";
$char++;
echo "<td>" . $row['Rp1'] . "</td>";
echo "<td>" . $row['Rp2'] . "</td>";
echo "<td>" . $row['Rp3'] . "</td>";
echo "<td>" . $row['Rp4'] . "</td>";
echo "</tr>";
}
?>
I tried below solution but it ended up with the same result.
<?php
$char= 'a';
for($i=0;$i<26;$i++)
while($row = mysqli_fetch_array($search_result))
{
echo "<tr>";
echo "<td>" .$char. "</td>";
$char++;
echo "<td>" . $row['Rp1'] . "</td>";
echo "<td>" . $row['Rp2'] . "</td>";
echo "<td>" . $row['Rp3'] . "</td>";
echo "<td>" . $row['Rp4'] . "</td>";
echo "</tr>";
}
?>
You could utilize PHP's built-in array functions for handling internal array pointers:
$range = range('a', 'z');
while ($row = mysqli_fetch_array($search_result)) {
echo "<tr>";
echo "<td>" . current($range) . "</td>";
echo "<td>" . $row['Rp1'] . "</td>";
echo "<td>" . $row['Rp2'] . "</td>";
echo "<td>" . $row['Rp3'] . "</td>";
echo "<td>" . $row['Rp4'] . "</td>";
echo "</tr>";
if (next($range) !== false) {
reset($range);
}
}
Checkout these references to learn more:
http://php.net/manual/en/function.current.php
http://php.net/manual/en/function.next.php
http://php.net/manual/en/function.reset.php
You can reference your array of letters by a calculated value, $i % 26, where $i is the zero based iteration count of your while loop.
$letters = range('a', 'z');
$i = 0;
while ($row = mysqli_fetch_array($search_result)) {
echo "<tr>";
echo "<td>" . $letters[$i++ % 26] . "</td>";
echo "<td>" . $row['Rp1'] . "</td>";
echo "<td>" . $row['Rp2'] . "</td>";
echo "<td>" . $row['Rp3'] . "</td>";
echo "<td>" . $row['Rp4'] . "</td>";
echo "</tr>";
}
Firstly, get the characters from a to z in an array.
Initialize a counter (index) to access a specific character from the array.
Increment the counter inside loop.
If the counter value goes above 25 (index starts from 0 - so this represents 26th array item), reset its value back to zero.
Try the following:
<?php
// get array for characters from a to z
$characters = range('a', 'z');
$counter = 0; // initialize the counter (index)
while($row = mysqli_fetch_array($search_result))
{
echo "<tr>";
// access the character using counter index
echo "<td>" . $characters[$counter] . "</td>";
// Increment the counter
$counter++;
// Check and reset if the counter has gone above 25 (26th array item)
$counter = ($counter > 25 ? 0 : $counter);
echo "<td>" . $row['Rp1'] . "</td>";
echo "<td>" . $row['Rp2'] . "</td>";
echo "<td>" . $row['Rp3'] . "</td>";
echo "<td>" . $row['Rp4'] . "</td>";
echo "</tr>";
}
?>
I am attempting to fetch data from my database using 8 different tables named:
inventory descriptorid typeid conditionid statusid locationid stationid users
I believe my query is working correctly the problem is using my $row variable. I am only getting the results from data in the inventory table. Can Someone explain how I can fetch my data from each table?
The code is below:
<?php
// get the records from the database
if ($result = $con->query("
SELECT inventory.InventoryID, descriptorid.dename, typeid.tyname,
inventory.Serial, inventory.ServiceTag, inventory.CityTag,
conditioncid.conname, statusid.statusname, locationid.locname,
stationid.stationname, users.Fname, users.Lname,
inventory.PurchaseDate, inventory.POnumber
FROM inventory
LEFT JOIN descriptorid
ON inventory.Descriptor = descriptorid.dename
LEFT JOIN typeid
ON inventory.Type = typeid.tyname
LEFT JOIN conditioncid
ON inventory.ConditionC = conditioncid.conname
LEFT JOIN statusid
ON inventory.Status = statusid.statusname
LEFT JOIN locationid
ON inventory.Location = locationid.locname
LEFT JOIN stationid
ON inventory.Station = stationid.stationname
LEFT JOIN users
ON inventory.cuserFname = users.Fname
AND inventory.cuserLname = users.Lname "))
{
// display records if there are records to display
if ($result->num_rows > 0)
{
// display records in a table
echo "<table border id='myTable' class='tablesorter' >";
// set table headers
echo "<thead><th>ID</th><th>Descriptor</th><th>Type</th><th>Serial</th><th>ServiceTag</th><th>CityTag</th><th>Condition</th><th>Status</th><th>Location</th><th>Station</th><th>CurrentUser</th><th>Lastname</th><th>PurchaseDate</th><th>POnumber</th><th></th><th></th></thead>";
echo "<tbody";
while ($row = $result->fetch_object())
{
// dump whats returned
// print_r($row);
echo "<tr>";
echo "<td>" . $row->InventoryID . "</td>";
echo "<td>" . $row->Descriptor . "</td>";
echo "<td>" . $row->Type . "</td>";
echo "<td>" . $row->Serial . "</td>";
echo "<td>" . $row->ServiceTag . "</td>";
echo "<td>" . $row->CityTag . "</td>";
echo "<td>" . $row->ConditionC . "</td>";
echo "<td>" . $row->Status . "</td>";
echo "<td>" . $row->Location . "</td>";
echo "<td>" . $row->Station . "</td>";
echo "<td>" . $row->cUserFname . "</td>";
echo "<td>" . $row->cUserLname . "</td>";
echo "<td>" . $row->PurchaseDate . "</td>";
echo "<td>" . $row->POnumber . "</td>";
echo "<td><a href='invrecords.php?InventoryID=" . $row->InventoryID . "'><img src='img/default/button_mini_ticket_edit.gif'></a></td>";
echo '<td><img src="img/default/button_mini_delete.gif">';
echo "</tr>";
}
echo "</tbody>";
echo "</table>";
You should name your row vars as you select them (as you did it with the columns of the inventory table):
Wrong:
echo "<td>" . $row->Descriptor . "</td>";
echo "<td>" . $row->Type . "</td>";
echo "<td>" . $row->ConditionC . "</td>";
echo "<td>" . $row->Status . "</td>";
echo "<td>" . $row->Location . "</td>";
echo "<td>" . $row->Station . "</td>";
echo "<td>" . $row->cUserFname . "</td>";
echo "<td>" . $row->cUserLname . "</td>";
Use instead:
echo "<td>" . $row->dename . "</td>";
echo "<td>" . $row->tyname. "</td>";
echo "<td>" . $row->conname. "</td>";
echo "<td>" . $row->statusname. "</td>";
echo "<td>" . $row->locname. "</td>";
echo "<td>" . $row->stationname. "</td>";
echo "<td>" . $row->Fname. "</td>";
echo "<td>" . $row->Lname. "</td>";
Are you sure that you want use LEFT JOIN instead of INNER JOIN? That means that if you have table A and B you only take elements which are located in A but not necessarily in B. That means if there is not a matching partner for an element of the left table, you would have a row containg an element of table A in one column and null in the other column.
This might be one reason for having a resultset only containing an element of the inventory table.
As far as I understand you want a result containing the elements joined on equal attributes, so try to replace LEFT JOIN with INNER JOIN.
Image source: http://www.codeproject.com/Articles/33052/Visual-Representation-of-SQL-Joins
Ok so the issue was instead of matching the third option (dename, tyname,etc.) on my secondary tables with the inventory I should've been matching it with the ID of that said table. Now when I query the third option with my row variable I can select any option from my secondary table (deid,tyid). It now outputs the desired results and hopefully I can create a drop down list using a similar query.Thanks for all the ideas.
I have a loop inside other loop which is not working, this is the code:
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['rowId'] . "</td>";
echo "<td>" . $row['startDate'] . "</td>";
echo "<td>" . $row['eventName'] . "</td>";
echo "<td>" . $row['betName'] . "</td>";
$string1 = "SELECT * FROM newCell WHERE rowId ='";
$string2 = $row['rowId']."'";
$result2 = $string1.$string2;
echo "<td>" . $result2 . "</td>";
while($row2 = mysqli_fetch_array($result2))
{
echo "<td>" . $row2['odds'] . "</td>";
echo "<td>" . $row2['outcomeName'] . "</td>";
}
echo "</tr>";
}
When I query $result2 directly into the BBDD for the first result it shows three results but the code doesn't go in the second LOOP. Why? Any error here?
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['rowId'] . "</td>";
echo "<td>" . $row['startDate'] . "</td>";
echo "<td>" . $row['eventName'] . "</td>";
echo "<td>" . $row['betName'] . "</td>";
$string1 = "SELECT * FROM newCell WHERE rowId ='";
$string2 = $row['rowId']."'";
$result2 = $string1.$string2;
echo "<td>" . $result2 . "</td>";
$result2 = mysqli_query($connection, $result2);
while($row2 = mysqli_fetch_array($result2))
{
echo "<td>" . $row2['odds'] . "</td>";
echo "<td>" . $row2['outcomeName'] . "</td>";
}
echo "</tr>";
}
Use:
$query = "SELECT ....";
$result2 = mysqli_query($db, $query);
while($row2 = mysqli_fetch_array($result2))
{
echo "<td>" . $row2['odds'] . "</td>";
echo "<td>" . $row2['outcomeName'] . "</td>";
}
Before read this How can I prevent SQL injection in PHP? topic. After try to use mysql_query()
Try This
<?php
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['rowId'] . "</td>";
echo "<td>" . $row['startDate'] . "</td>";
echo "<td>" . $row['eventName'] . "</td>";
echo "<td>" . $row['betName'] . "</td>";
$string1 = "SELECT * FROM newCell WHERE rowId ='";
$string2 = $row['rowId']."'";
$result2 = $string1.$string2;
echo "<td>" . $result2 . "</td>";
$results = mysqli_query($db,$result2);
while($row2 = mysqli_fetch_array($results))
{
echo "<td>" . $row2['odds'] . "</td>";
echo "<td>" . $row['outcomeName'] . "</td>";
}
echo "</tr>";
}
?>
Before fetching data execute mysql query using mysqli_query() function then run mysqli_fetch_array(). It would be good if you count result as $count = mysqli_num_rows($query) and manage your code with if... else .
I think $result2 should be output of mysqli_query not just merely query. Talking in analogous to MySQL.
Probably you should have something like this
$result2 = mysqli_query($result2);
I have a database table with student room assignments. Each student has a specific hall, floor, and apartment. I need to display each student in a specific table so the results look like a floor layout. Below is an example. The student ID needs to be in the correct Apartment slot. There could be several ID's per apartment. Right now it just lists them down the page.
Apartment 102 Apartment 101
Apartment 104 Apartment 103
Apartment 106 Apartment 105
$query = "select res.ID_NUM as ID, res.APARTMENT
From Residents res
Where res.sess_cde = '$pulledsession'
and res.ROOM_ASSIGN_STS = 'A'
and res.BLDG_CDE = '$pulledhall'
and res.FLOOR = '$pulledfloor'";
$result = odbc_exec($connect, $query);
echo "<table style='padding:25;'>
<tr>
<th>Apartment</th>
<th>ID</th>
</tr>";
while(odbc_fetch_row($result)){
$ID = odbc_result($result,ID);
$APARTMENT = odbc_result($result,APARTMENT);
if ($APARTMENT == $pulledfloor.'01')
{
echo "<tr >";
echo "<td>" . $pulledfloor.'01' . "</td>";
echo "<td>" . $ID . "</td>";
echo "</tr>";
}
else if ($APARTMENT == $pulledfloor.'02')
{
echo "<tr>";
echo "<td>" . $pulledfloor.'02' . "</td>";
echo "<td>" . $ID . "</td>";
echo "</tr>";
}
else if ($APARTMENT == $pulledfloor.'03')
{
echo "<tr>";
echo "<td>" . $pulledfloor.'03' . "</td>";
echo "<td>" . $ID . "</td>";
echo "</tr>";
}
else if ($APARTMENT == $pulledfloor.'04')
{
echo "<tr>";
echo "<td>" . $pulledfloor.'04' . "</td>";
echo "<td>" . $ID . "</td>";
echo "</tr>";
}
else if ($APARTMENT == $pulledfloor.'05')
{
echo "<tr>";
echo "<td>" . $pulledfloor.'05' . "</td>";
echo "<td>" . $ID . "</td>";
echo "</tr>";
}
else if ($APARTMENT == $pulledfloor.'06')
{
echo "<tr>";
echo "<td>" . $pulledfloor.'06' . "</td>";
echo "<td>" . $ID . "</td>";
echo "</tr>";
}
}
echo "</table>";
You would need to retrieve the room number as well. After that, one way to do it would be to load the results into an associated array:
$rooms[$row['roomNumber']] = $resName; // Example
Once you have the array built, then you can echo it back out into a table, either manually (build the full table and echo each one with
echo "<tr><td>".$rooms['102']."</td><td>".$rooms['101']."</td></tr>";
or similar, or do it dynamically by incrementing two room numbers in a loop.
If you have multiple students in a room, then tack on more depth to the array:
$rooms[$row['roomNumber']][] = $resName;
Then use a loop in each cell to echo it back out.
echo '<td><input type="checkbox" name="items[]" value="' . $row['0'] . '" /></td>';
Hi, I'm trying to get a reference number which comes from a an array called items from another page as shown above, and to find it in the table and print out the reference row, like "Title,Platform...." into another table, but I can't seem to get it working...any help be appreciated
if (isset($_POST['items'])) {
$n = count($_POST['items']);
for($i=0; $i < $n; $i++){
// echo $_POST['items'][$i];
}
$items = array();
foreach ($_POST['items'] as $item) {
$items[] = pg_escape_string($con, $item);
}
if (!$_SESSION["selectingrows"]) {
$item_string = "'" . implode("','", $items) . "'";
$result = pg_query ($con, "SELECT title, platform, description, price FROM CSGames WHERE 'refnumber' IN ($item_string)");
while($rows = pg_fetch_assoc($result))
{
echo "<tr>";
echo "<td>" . $rows['1'] . "</td>";
echo "<td>" . $rows['2'] . "</td>";
echo "<td>" . $rows['3'] . "</td>";
echo "<td>" . $rows['4'] . "</td>";
echo "</tr>";
}
}
}
One thing, you need to put {} braces after your while loop.
Here is what you are doing:
while($rows = pg_fetch_assoc($result))
echo"<tr>"; echo "<td>" . $rows['1'] . "</td>"; echo "<td>" . $rows['2'] . "</td>"; echo "<td>" . $rows['3'] . "</td>"; echo "<td>" . $rows['4'] . "</td>";
echo"</tr>";
By not putting braces around the code after the while statement, here is what your code really does:
while($rows = pg_fetch_assoc($result))
{
echo"<tr>";
}
echo "<td>" . $rows['1'] . "</td>"; echo "<td>" . $rows['2'] . "</td>"; echo "<td>" . $rows['3'] . "</td>"; echo "<td>" . $rows['4'] . "</td>";
echo"</tr>";
You should always put braces in to define what code is in the while loop.
You want your code to be something like this:
while($rows = pg_fetch_assoc($result))
{
echo "<tr>";
echo "<td>" . $rows['1'] . "</td>";
echo "<td>" . $rows['2'] . "</td>";
echo "<td>" . $rows['3'] . "</td>";
echo "<td>" . $rows['4'] . "</td>";
echo "</tr>";
}
Format your code neatly and properly. By doing this your code is clearer and it is much easier to notice possible mistakes like the above. Always use braces for if, while, for statements. When putting an end line semicolon ; put in a new line break. Indent your code correctly. It's little things like formatting that make coding easier.
Now the next problem I can see is the values you are getting from the $rows array:
$rows['1'];
$rows['2'];
$rows['3'];
$rows['4'];
This is trying to get something from the $rows array which has the key of string '1'.
Usually you access array values by index, which uses an integer beggining from 0. Or you access it by a key.
Either you can try this:
$rows[0];
$rows[1];
$rows[2];
$rows[3];
Or this:
$rows['title'];
$rows['platform'];
$rows['description'];
$rows['price'];