Here's my code:
now, how can I save these in session, so that whenever I go to new link, the table is still there. Thanks guys.
while($row = mysqli_fetch_assoc($locate)){
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['grade'] . "</td>";
echo "<td>" . $row['section'] . "</td>";
echo "<td>" . $row['subject'] . "</td>";
$namers = $row['name'];
//echo "<td><center> <button name = 'name1' type = 'submit' value = '$namers'>Go</button> </center></td>";
Either store $row in a session:
$_SESSION['rowdata'] = $row;
OR serialise the $row to store it and unserialise it when you want to use it:
//Store
$_SESSION['rowdata'] = serialise($row);
//get back
$row = unserialise($_SESSION['rowdata']);
Related
I have created a database in phpMyAdmin and I have 10 rows of data and each row has a column to show its website address. I want to make the website address clickable.
I used the below array method to show my website address link, but it doesn't work
$website = array(
array("Google","https://code.tutsplus.com"),
array("Bing","https://weatherstack.com"),
array("W3","https://www.w3schools.com")
);
foreach ($website as $urlitem){
echo "<a href='".$urlitem[1]."'></a>";
}
// this gets an associative array (ie the keys can be used as well as the indicies)
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
// The below code displays my table
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['type'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['street'] . "</td>";
echo "<td>" . $row['city'] . "</td>";
echo "<td>" . $row['region'] . "</td>";
echo "<td>" . $row['code'] . "</td>";
echo "<td>" . $row['phone'] . "</td>";
echo "<td><a href='".$urlitem[0]."'>" .$row["website"] ."</td>";
echo "</tr>";
}
}
In the foreach loop, the issue is you are not print out anything to be displayed for anchor tag. Means you anchor tag exists but it does not have any content to be shown on html;
YOUR CONTENT MISSING PART
After that in your while loop, you are using the variable $urlitem which does not exists. You either need to declared it outside your foreach or need to use the $website variable to get the url.
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>";
}
}
So i got a little problem i just cant figure out to solve..
So i got a While loop that is printing from my database:
$resultArray = array();
$sql = "SELECT * FROM Kunde WHERE fornavn LIKE '%".$searchq."%' ";
$stmt = sqlsrv_query( $conn, $sql);
if ($stmt === false){
die( print_r(sqlsrv_errors(), tue) );
}
echo "<table border='1'>";
echo "<tr><th>Kundenr</th><th>Fornavn</th><th>Etternavn</th><th>Tlf</th><th>Epost</th><th>Produktnr</th><th>Adresse</th></tr>";
while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_BOTH))
{
$resultArray[] = $row;
echo "<tr>";
echo ''.$row[0].'';
echo "<td>" . $row[1] . "</td>";
echo "<td>" . $row[2] . "</td>";
echo "<td>" . $row[3] . "</td>";
echo "<td>" . $row[4] . "</td>";
echo "<td>" . $row[5] . "</td>";
echo "<td>" . $row[6] . "</td>";
echo "</tr>";
I now want to send the variable i press to a new php file. To do this i am using:
session_start();
$_SESSION['kundenr'] = $resultArray[0]['kundenr'];
The problem is that i my $resultArray i must write a number inside [], which define what loop number it are saving ['kundenr'] from. So now i have [0], it is always sending ['kundenr'] from the first loop. This works fine as long I dont have 10 search result to choose from.
So are here anyone that now a easy fix? somthing to store the loop value from my click or something?
Thanks!
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);
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'];