I want to have the following struct on my print: (https://i.stack.imgur.com/e16ya.png)
My problem is that i want to all Nomos to be printed together for every Perifereia.
my code is:
$sql = pg_query($conn,"SELECT Όνομα\_Περιφέρειας,
Όνομα\_Νομού,
Όνομα\_Δήμου,
Γεωγραφικό\_Μήκος,
Γεωγραφικό\_Πλάτος
FROM Δήμοι
ORDER BY Όνομα\_Περιφέρειας,Όνομα\_Νομού,Όνομα\_Δήμου ASC
");
while ($row = pg_fetch_row($sql)) {
echo "Περιφέρεια $row\[0\]";
echo ("\\n");
echo " Νομός $row\[1\]";
echo ("\\n");
echo " Δήμος $row\[2\]: $row\[3\],$row\[4\]";
echo ("\\n");
}
now my result is (https://i.stack.imgur.com/Avunw.png)
Related
I am trying to combine 2 tables of data. However, all of my results are getting stuck in one set of results. I need the results to go across multiple.
Here is what Is happening:
<?php
$con = new mysqli("mysql.hostinger.co.uk", "-", "-", "-");
$con->set_charset('utf8mb4');
//Retrieve all the data from the Main Data Table
$result = $con->query("SELECT StreamDayID, WeekColor, DayName, StreamTitle, StreamGame, TIME_FORMAT(StartTime, '%r') AS StartTime, DATE_FORMAT(StreamDate, '%D %b') AS StreamDate FROM `WeekData` WHERE WeekColor='Blue'") or die(mysql_error());
//Retrieve all the data from the Joiners Table
$joinresult = $con->query("SELECT JoinID, JoinedBy, StreamID FROM `JoinedBy` INNER JOIN `WeekData` ON StreamID = StreamDayID") or die(mysql_error());
echo "<div id='blue' class='day'>";
echo "<h1 class='row'>Blue:</h1>";
//keeps getting the next row until there are no more to get
while($row = mysqli_fetch_array($result)){
//Print out the contents of each row into a table
echo "<div id='blue";
echo $row['StreamDayID'];
echo "' class='les'>";
echo "<div class='title'>";
echo "<h2>";
echo $row['StreamTitle'];
echo "</h2>";
echo "</div>";
echo "<div class='detail'>";
echo "<h4>Day: ";
echo $row['DayName'];
echo "</h4>";
echo "<h4>Game: ";
echo $row['StreamGame'];
echo "</h4>";
echo "<h4 class='joiners'>Joined By:";
while($join = mysqli_fetch_array($joinresult)){
echo "<a class='delink' href='https://twitch.tv/";
echo $join['JoinedBy'];
echo "'>";
echo $join['JoinedBy'];
echo "</a>";
};
echo "</h4>";
echo "<h4>Start Time: ";
echo $row['StartTime'];
echo "</h4>";
echo "<h4>Stream Date: ";
echo $row['StreamDate'];
echo "</h4>";
echo "</div>";
echo "</div>";
};
?>
Here is What I want to happen:
It seems like your $joinresult needs to be defined within the first while loop. This will allow you to filter the JoinedBy table on the current StreamID.
Currently you're returning every row in JoinedBy as there is no filtering.
To filter by the StreamID I would suggest using a prepared statement and setting a filter in the WHERE statement:
...
while($row = mysqli_fetch_array($result)){
// initialise the statement
$stmt = $con->stmt_init();
$stmt->prepare("SELECT JoinID, JoinedBy, StreamID FROM `JoinedBy` WHERE StreamID = ?");
// if `StreamDayID` is an integer, change "s" -> "i"
$stmt->bind_param("s", $row["StreamDayID"]);
$exec = $stmt->execute();
if(!$exec){
// error
die(mysql_error());
}
$joinresult = $stmt->get_result();
...
echo "<h4 class='joiners'>Joined By:";
while($join = mysqli_fetch_array($joinresult)){
...
NOTE: You don't have to use a prepared statement in this case as you know the value (i.e. SQL injection unlikely), BUT it is good practice to do so.
I tried to echo rows from mysql database. So in my code user can enter a value, and then program echoes table around the entered value. That part of the code works fine, but when I tried to echo entered values and corresponding row to the value, it somehow gave me first row. While I needed exact row, which I previously entered.
Here is my code:
$density = $_POST["density"];
$t = $_POST["t"];
$query = $pdo->prepare ("SELECT * FROM mytable WHERE "
."(Density >= '$density'-0.05 AND Density <='$density' +0.05) AND "
."(Temp >='$t' -2 AND Temp <='$t' +2) ");
if (!$query->rowCount() == 0) {
$query -> execute();
while ($results = $query->fetch(PDO::FETCH_ASSOC)){
echo "<table>";
echo "<tr><td>";
echo $results['Temperature'];
echo "</td><td">";
echo $results['Density'];
echo "</td><td>";
echo $results['DensityValues'] + $delden;
echo "</td></tr>";
echo "</table>";
}
}
So currently it prints the first row which is: Temp=>10, Density=>600, DensityValues=>658;
While I need to get values I have enterd and coresponding density value: Temp=>12, Density=>650, DensityValues=>678;
I am updating all my code to Mysqli, before I did I had this code and it worked:
while($row = mysql_fetch_assoc($WildHorses)){
$InWild[] = $row['id'];
}
$RandomWild = array_rand($InWild);
$RandomHorse = $InWild[$RandomWild];
This is my SELECT statement:
$sql = "SELECT Horse.id, Horse.Name, Horse.Age, Horse.Image_name, Horse.Owner, Horse.Barn, Images.Image_path, Images.Image_name FROM Horse, Images WHERE Horse.Owner = '$colname_WildHorseList' AND Images.Image_name = Horse.Image_name";
$result = mysqli_query($con,$sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo "id: " . $row["id"]. " Name: " . $row["Name"]. " ImageName: " . $row["Image_name"]. "<br>";
}
} else {
echo "0 results";
}
The SELECT statement ends up echoing all of the correct information, but I want to make an array of only the Id's so that I can pick one at random each time a button is clicked.
I have tried multiple different copies and pastes of code to try and get what I want, but nothing seems to come out right.
Can someone point me in the right direction or explain what I'm doing wrong?
In your while loop you can simply do this :-
$i=0;
$animals=array();
$animals[$i]=$row["id"]; //puts id in array
And then you can create a random number by "rand()" between the length of 0-$i
and can get the job done.
I am trying to show all the whiskies from my database with the dates alongside it , from the URL. I have several whiskies iwth the same names and want to show the price changes over the years. My current code is as below , but at the moment it is just showing one date , name and price. I am happy to show the one name at the top, but I would like to show all the prices and dates. test_db
if (isset($_GET['id'])) {
$sql = "SELECT date , name , price FROM test_db WHERE name = :id ORDER BY name ASC";
$stmt = $conn->prepare($sql);
$stmt->execute( [ 'id' => $_GET['id'] ] );
$row = $stmt->fetch();
echo "<div class='details'>";
echo "<br>";
echo $row['name'];
echo "<br><br>";
echo $row ['date'];
echo"<br><br>";
echo " £";
echo floor ($row ['price']);
echo "<br>";
echo "<br>";
echo "<br>";
echo "</div>";
if you have several result you should use a loop eg:
while( $row = $stmt->fetch() ) {
echo "<div class='details'>";
echo "<br>";
echo $row['name'];
echo "<br><br>";
echo $row ['date'];
echo"<br><br>";
echo " £";
echo floor ($row ['price']);
echo "<br>";
echo "<br>";
echo "<br>";
echo "</div>";
}
I'm having problems with the Array to string conversion error.
It's occurring here:
<div id="pagename">
<?php echo ['SELECT name FROM hjemmesider']; ?>
</div>
I'm trying to fetch a row from my db and display it, but i can't make it work... I have tried tons of things to try and sort it out, and of course researched it a great bit on the internet.
in your foreach or while use echo $row['your_field_name'];
You are not doin it right.. try like this:
$sql = "SELECT id, firstname, lastname FROM MyGuests";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";
}
} else {
echo "0 results";
}
It most probably means that you are trying to echo an Array. Use print_r function on your result and things will get clearer.