2

Problem - I've created a DB Connection, I've queried the DB, and I've turned the results into an associative array. Displaying the SQL Data using PHP is the trouble I'm having. I've managed to display it in a basic way, however my aim is for this information to display as a 'News Column' down the side of the page, and therefore I need the data to be inserted into Divs for me to be able to manipulate.

Attempt thus far - As one can see below, the presentation of this attempt to display my data is cringeworthy. However in this way I did manage to distinguish the types of headings for each piece of data.

    echo "<h2>" . $row["title"]. "</h2><h3>Date: " . $row["dateTime"]. "</h3><h4>Passage: " . $row["passage"]. "</h4><br>";

HTML For Current Webpage:

https://i.sstatic.net/Fb26J.png (What my 'News Column' looks like) https://i.sstatic.net/rlRB4.png (My HTML Code I want Data to be inputted into) - The aim is for someone to be able to complete this form (https://i.sstatic.net/fVA1g.png) and the data to go into the DB (done this), and display in the column with the 'Title' appearing in the right place, same with 'Date' and finally with the 'Passage'.

What I've Found Online:

I dislike wasting people's time so I've spent a few hours searching online. Thus far I've found mostly people creating HTML Tables with their SQL Data, which is great and all, however I've not found anyone who's using Divs/Headings/Paragraphs etc...

My CSS:

I have a CSS File which I'm retrieving (or whatever the term is) via HTML Code which is working (so far).

2
  • 1
    So if I understand your question correctly, you want to display the data you retrieved via PHP/SQL in a certain HTML format? Commented Feb 2, 2016 at 22:43
  • 1
    Could you paste your code to pastebin, a picture is not really good to work with. Commented Feb 2, 2016 at 22:47

1 Answer 1

1

Interestingly, you seem to have all the components you need: An example of how the markup (the HTML) needs to be outputted and the DB query and data to inject into it. Unless I'm missing something (it's always possible!), then all that remains is for you to glue the two together.

Assuming the form represented in your most recent graphic is generated within a .php file you can either pre-build a giant variable containing all the markup necessary to render your output, or write some static HTML and insert only the dynamic elements using native PHP (PHP is itself a templating language of sorts).

Using the latter method you'd do something like the following (forgive different CSS class names and tag hierarchy, I can't copy/paste from a PNG :-)

<div class="wrapper"> <?php foreach($myResult as $row): ?> <div class="article> <h3><?php echo $row['Title']; ?></h3> <div class="date"><?php echo $row['Date']; ?></div> <p><?php echo $row['Body']; ?></p> <div> <?php endforeach; ?> </div>

Sign up to request clarification or add additional context in comments.

1 Comment

Just wanted to say thank you. At first I was baffled but after a thorough examination it makes perfect sense and is working!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.