Hi all,
I’m learning php by basically doing it the idots way. I work with developers and have always been curious. I wanted to create a specific feature and thought I would give it a go and they could help me.
- I wanted a form, once filled and submitted that data to be saved in a mysql database.
- multipe rows of data but the ID i wanted to search for wasn’t the key. In fact, there was no key. i.e
(name, last name, car model) so each row could contain multiple duplicate values.
I then wanted a page to have a cell info in the url. The php to use that ‘cell info’ to bring all of the rows that match that.
(so url.com/get.php?id=name)
So all of the rows with ‘name’ in the first row will be displayed.
Amazingly I managed to get this to work via actually hours of google and online tips and tricks.
I then wanted the output to look pretty.
So i learned about using html and php in one file.
Using $variable = ) etc.
The entire block of code that parses the ID from the url, checks the database and echos the rows is as follows:
$conn = new mysqli($servername, $username, $password, $db_name);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
//$name = " . $row["reg"]. ";
$sql="SELECT * from table where name = '".$_GET["id"]."' ";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
"name: " . $row["name"]. " - car model: " . $row["car_model"];
}
}
else {
echo "0 results";
}
$conn->close();
?>
Through the great teacher that is google I made great progress.
I then wanted to add html table with css to make it all pretty.
I learned that i could add <style>
before the <?php tag and html tables after the close tag.
I learned that I could then use
$first = “” . $row[“reg”];
to create a variable with the info from the database and echo it in my table cells in html.
<?php echo $first; ?>The problem is, with the
while($row = $result->fetch_assoc()) {
"name: " . $row[“name”]. " - car model: " . $row[“car_model”];
section of code, the php displays all rows that contain the information that i want automaticaly.
But when i do
$first = “” . $row[“reg”];
and echo it, it will only show the values from the very first rows.
How do i attach a $variable to a second row of data?
Sorry if i use bad terminology i am literally just starting out. You have no idea how many hours i have spend on trial and error instead of just taking the time to learn this properly.