- This topic is empty.
-
AuthorPosts
-
September 12, 2014 at 4:00 pm #182818__Participant
I’m looking for a way to use my query results after the $stmt is closed, don’t even know if its possible.
The simplest possible approach would be to do everything as before, but instead of
echo
ing the data, assign it to a variable for later use.// no while( $row = $result->fetch() ){ // stuff everything into your mouth at once echo "Hello, {$row['name']}!"; } // yes while( $row = $result->fetch() ){ // save it for later $names = $row['name']; } foreach( $names as $name ){ // enjoy at your leisure echo "Hello, $name!"; }
But we need more specific info for more specific answers.
September 12, 2014 at 6:33 pm #182829__ParticipantWhat is the difference from the while loop and the foreach loop?
while
loops for as long as its condition is truthy.
foreach
loops once for each item in an array or object.However, this is not the point of what I was describing. It is only tangentially related. I was trying to illustrate assigning data to variables for later use, rather than assuming you must output it immediately when you fetch it.
how would I loop through that to echo out all the forum posts later?
The same way I do in my example.
- fetch the results
- assign the data from each row to a variable
- afterwards, use the data when/as needed
I’m really not sure what you are asking about. Let’s take another example: say you need two queries, perhaps one for “names” and one for “places.”
$SQL_getNames = "select name from person limit 10"; $SQL_getPlaces = "select place from location limit 10";
Using prepared statements, you can’t run the second without closing the first, but you want to output the results together. So, run the first query. Do not output the results; just fetch them and save them for later.
/* do prepared statement for $SQL_getNames */ while( $row = $stmt1->fetch() ){ $names[] = $row['name']; }
edit
It’s like eggs. You don’t have to break open the egg and cook it as soon as it comes out of the chicken. You can stick it in a tray and put it in the fridge for later.
/editThen do the next query.
/* close first statement */ /* do prepared statement for $SQL_getPlaces */ while( $row = $stmt2->fetch() ){ $places[] = $row['place']; } /* close second statement */
Then use the data when you are ready to output.
foreach( $names as $i=>$name ){ echo "Hello, $name! You're travelling to {$places[$i]}."; }
If query one and query two are dependent on each other, you might be able to do a join instead:
$SQL = "select p.name name, l.place place from person p join location l on p.hometown = l.place"; // . . . while( $row = $stmt->fetch() ){ echo "Hello, {$row['name']}! You live in {$row['place']}."; }
But I don’t know if any of this applies to your situation.
Like I said, we need more specific info for a more specific answer.September 12, 2014 at 10:26 pm #182836__ParticipantI don’t understand where the I is coming from in the foreach loop?
You mean,
$i
? You can useforeach
in two forms: one assigns a variable to the value, the other additionally assigns a variable to the key. Read up.Is there any real advantage when using SELECT statements of using Prepared statements? I know they protect again sql attacks when inserting data to the DB, what about selecting from.
Refer back to earlier in our conversation.
September 14, 2014 at 2:08 pm #182988chrisburtonParticipantWould have to see what connect.php entails. However, I like how Chris Coyier sets up his CSS.
For example (he uses Scss but you can do this without it):
- layout.css
- typography.css
- colors.css
- etc…
and then imports all the above to a
global.css
file.September 14, 2014 at 2:16 pm #182990chrisburtonParticipantglobal.css file:
@import 'layout.css'; @import 'typography.css'; @import 'colors.css';
September 14, 2014 at 2:24 pm #182992__ParticipantI was thinking of using the connect.php page to link to the Central style sheet, because virtually every singe page on the site is connected to my ‘connect.php’ page.
[emphasis added]Are you talking about the script that handles your database connection?
If so, no, that is not a good place for printing out HTML. Doing so offers very little control over when/if output is sent to the browser. This is a job that belongs to a View controller, or a template.It’s best to not output anything until everything is ready, and then have a single object/function/script/template/whatever-you-choose that does it — in a controllable, predictable way.
September 14, 2014 at 2:25 pm #182994chrisburtonParticipantI would have to agree with traq on this.
September 14, 2014 at 2:29 pm #182997__ParticipantIt would not ‘print’ out any HTML, it would only be linked to a stylesheet
I’m not sure what you mean by this… I had originally interpreted this as “output an HTML
<link>
that references the stylesheet(s).” Can you explain in a bit more detail?In any case, even if this does not generate any output, it’s best to not mix jobs like this. You’ll be stuck establishing a DB connection, even if you know you don’t need one, just to make sure you have a stylesheet. And what if you need a DB connection, but not the stylesheet isn’t the one you need for the current page? Different things should be in different scripts.
September 14, 2014 at 2:43 pm #183001chrisburtonParticipantCreate a header.php and place all head stuff in it (doctype, css, etc), same thing for footer.php (javascript, closing body/html tags). Use an include for them.
September 14, 2014 at 2:51 pm #183004chrisburtonParticipantYep. I have to run but I’ll check back in later.
September 14, 2014 at 2:56 pm #183005__Participantbasically what I am trying to do is bring all my CSS and javascript into a central organized place. So I was going to make a global.css master stylesheet and then make a few sub style sheet that will be imported to the master.
This part is fine. Fantastic. And just to be clear, I didn’t mean to contradict anything @chrisburton said; it’s all great advice.
In order to link the stylesheet to every singe page on the site, I was going to make a link on the connect.php page
[emphasis added]That’s still tripping me up.
What do you mean by “make a link,” if not “output a<link>
“?what do you suggest I do, in any event I want to have one global stylesheet.
For what you’re doing, and how your code is currently structured (i.e., no crazy-crazy refactoring that would get you completely lost), you would probably be best off making a template. The idea being:
- don’t output (
echo
, etc.) anything inside your main code. - whatever your code wants to output should be assigned to a variable instead.
- when everything else is done, use a template to display the output in the desired HTML format.
I’m trying to decide if it would be best to give an object-oriented example, or try to adapt this to procedural code. But here’s the basic idea (not a working example; lots of stuff left out):
make-article.php
<?php require_once 'connection.php'; $result = $con->query( 'select title, author, content from article where id=1' ); $articleInfo = $result->fetch(); $article = "<article> <header> <h1>{$articleInfo['title']}</h1> <p>by {$articleInfo['author']}</p> </header> <p>{$articleInfo['content']}</p> </article>"; // DO NOT output // when you are ALL DONE, use the template instead require 'template.php';
template.php
<!doctype html> <html> <head> <meta charset=utf-8> <title>my article</title> <link href=/my/global.css rel=stylesheet type=text/css> </head> <body> <?php echo $article; ?> </body> </html>
September 14, 2014 at 3:03 pm #183008__ParticipantThat would be one way to do it, yes. It’s also the way that (I think) would require the least restructuring of your current code.
—BenWalkerOk im a little confused as to what you mean by a template
—drose379We’re all posting pretty much at once, here, so to clarify, here’s my comment on @chrisburton’s suggestion (which I did not see until after I posted my own). I’m pretty sure @BenWalker is referring to chris’ suggestion, not mine (though I think it’s good to start thinking about both concepts, and which might be better in the long term).
Create a header.php and place all head stuff in it (doctype, css, etc), same thing for footer.php (javascript, closing body/html tags). Use an include for them.
This works also, and is usually “just fine.”
For more complex code —i.e., if it is more of an “application” than a “page”— it’s better to separate out the “programming logic” from the “display” parts of the code.
To compare and contrast, this is like:
- common markup (top)
- php scripting + php output (page content)
- common markup (bottom)
While my example is like:
- php scripting
- common markup + php output (page content)
September 14, 2014 at 3:09 pm #183010__ParticipantI’ll see about writing a general-purpose
Response
class at some point.September 14, 2014 at 3:26 pm #183013chrisburtonParticipant@traq I agree with your overall method but I believe the HTML should not be stored inside the
$articleInfo
variable. I think it would be best to only have the text data inside of that and leave the HTML during output in case drose wants to add things later.<?php require_once 'connection.php'; $result = $con->query( 'select title, author, content from article where id=1' ); $article = $result->fetch(); $articleTitle = $article['title']; $articleContent = $article['content']; //etc // DO NOT output // when you are ALL DONE, use the template instead require 'template.php';
Therefore:
<!doctype html> <html> <head> <meta charset=utf-8> <title>my article</title> <link href=/my/global.css rel=stylesheet type=text/css> </head> <body> <h1><?php echo $articleTitle; ?></h1> ...etc </body> </html>
or better yet:
<!doctype html> <html> <head> <meta charset=utf-8> <title>my article</title> <link href=/my/global.css rel=stylesheet type=text/css> </head> <body> <h1><?php echo $article->title(); ?></h1> ...etc </body> </html>
September 14, 2014 at 3:35 pm #183015__ParticipantI believe the HTML should not be stored inside the $articleInfo variable. I think it would be best to only have the text data inside of that and leave the HTML during output in case drose wants to add things later.
Absolutely. If this were all wrapped up in an object, that would be much easier.
-
AuthorPosts
- The forum ‘Back End’ is closed to new topics and replies.