guymclarenza Posted February 23, 2021 Share Posted February 23, 2021 (edited) I know this function will get one result, I suspect foreach is the wrong loop to use, should I be using a loop at all? Can someone correct me please. public function getSites($site) { $sql = "SELECT * FROM website WHERE website_id = ?"; $stmt = $this->connect()->prepare($sql); $stmt->execute($site) $siten = $stmt->fetch(); foreach ($siten as $row) { $title = $row["website_title"]; $wname = $row["website_name"]; $image1 = "header.jpg"; $metadesc = $row["website_description"]; $wurl = $row["website_url"]; $gtag = $row["website_gtag"]; $gmap = $row["website_map"]; $wcss = $row["website_css"]; } } Edited February 23, 2021 by guymclarenza Quote Link to comment https://forums.phpfreaks.com/topic/312197-for-loops-learning/ Share on other sites More sharing options...
requinix Posted February 23, 2021 Share Posted February 23, 2021 Are you having a specific problem with $title and $wname and all those not being set? Errors about invalid offsets? Loops are for multiple things, right? Because the whole point of the loop is that it does something multiple times. So if you only have one row then it would make sense that you do not need a loop. fetch() returns a row - presumably an array, given that code you already have. Try removing the foreach (and adjusting the variable names) and seeing what happens. Quote Link to comment https://forums.phpfreaks.com/topic/312197-for-loops-learning/#findComment-1584702 Share on other sites More sharing options...
guymclarenza Posted February 23, 2021 Author Share Posted February 23, 2021 I haven't tested yet, It was a general question about coding practice. Is it as simple as not looping it? Quote Link to comment https://forums.phpfreaks.com/topic/312197-for-loops-learning/#findComment-1584703 Share on other sites More sharing options...
requinix Posted February 23, 2021 Share Posted February 23, 2021 Probably. If not then it's really close. The foreach loop would be more used with ->fetchAll() - if you had multiple rows, of course. Quote Link to comment https://forums.phpfreaks.com/topic/312197-for-loops-learning/#findComment-1584704 Share on other sites More sharing options...
guymclarenza Posted February 23, 2021 Author Share Posted February 23, 2021 Thank you, That was what I thought but was unsure about how to access the data without a loop. I am busy rebuilding an old package as OOP to improve it. The original works but I am concerned about security even though I have updated to PDO. I figure the more secure the better. It's just that sometimes this old brain of mine finds some concepts difficult. Quote Link to comment https://forums.phpfreaks.com/topic/312197-for-loops-learning/#findComment-1584705 Share on other sites More sharing options...
guymclarenza Posted February 23, 2021 Author Share Posted February 23, 2021 (edited) Just one more question, would it be better to use the loops on the page that is viewed or hide them in the functions? In the example above I have one row, In another I have multiple rows dependant on information from a different query. example. for each Get id, name from table idvar echo name Get field1, field2 from anothertable where field3 = idvar for each echo field1 and field 2 I am thinking that an SQL statement inner joining the two tables and grouping them by table.name may be a more elegant solution, yet I am not sure how not to repeat table.name on every iteration of anothertable.result Am I on the right track? The output should look like this table.name.1 anothertable.result.1 anothertable.result.2 anothertable.result.3 table.name.2 anothertable.result.1 anothertable.result.2 anothertable.result.3 etc... Edited February 23, 2021 by guymclarenza Quote Link to comment https://forums.phpfreaks.com/topic/312197-for-loops-learning/#findComment-1584706 Share on other sites More sharing options...
Solution requinix Posted February 23, 2021 Solution Share Posted February 23, 2021 21 minutes ago, guymclarenza said: Just one more question, would it be better to use the loops on the page that is viewed or hide them in the functions? A loop isn't a mystical thing that does work for you. It's merely a tool. A means to an end. So the question you should be asking is whether something, a page or a function or whatever, needs loops to perform its necessary tasks. And odds are that if you have more that one of some things then you'll need a loop to process them. 21 minutes ago, guymclarenza said: In the example above I have one row, In another I have multiple rows dependant on information from a different query. example. for each Get id, name from table idvar echo name Get field1, field2 from anothertable where field3 = idvar for each echo field1 and field 2 I am thinking that an SQL statement inner joining the two tables and grouping them by table.name may be a more elegant solution, yet I am not sure how not to repeat table.name on every iteration of anothertable.result So you have a one-to-many relationship? That is, every single row in "table" has one or more corresponding rows in "anothertable"? You're absolutely right to consider an INNER JOIN. It will be far, far more efficient for you and your database if you issued one single query to retrieve all the data at once instead of one for the first table and another N for the second table. But you're also right that it won't be obvious where one id/name stops and one id/name begins... ...unless you do what is probably going to sound obvious in retrospect: make the query also return the id and name. SELECT t.id, t.name, at.field1, at.field2 FROM table t JOIN anothertable at ON t.id = at.field3 ORDER BY t.name, at.something When you go through those results (with a loop), track the previous id and keep an eye out for when it changes. Note that sorting the results is key for this to work, since otherwise there's no particular guarantee on whether all the rows for a given table.id show up together, but it's also probably something you'd want to do anyways. Essentially, previous id = null foreach row { if current id != previous id { new heading } print row previous id = current id } In practice it tends to be a little more complicated than that, depending on what kind of output you want - especially if it's HTML, and especially especially if you want syntactically valid HTML. Quote Link to comment https://forums.phpfreaks.com/topic/312197-for-loops-learning/#findComment-1584707 Share on other sites More sharing options...
guymclarenza Posted February 23, 2021 Author Share Posted February 23, 2021 I am able to order the rows by the field required $sql = "SELECT table_cats.tablec_name, tables.table_question, tables.table_answer FROM table_cats INNER JOIN tables ON table_cats.tablec_id = tables.table_cat WHERE table_cats.tablec_site = ? ORDER BY table_cats.tablec_name"; $stmt = $this->connect()->prepare($sql); $stmt->execute($site); $fcid = $stmt->fetchAll(); foreach ($table as $table) { $fcat = $table["tablec_name"]; $fquest = $table["table_question"]; $fans = $table["table_answer"]; } , My HTML is not too crap, been doing it for a while, CSS though has it's challenges but not quite as many as PHP while I am learning new stuff. Your help is appreciated, The SQL above gives me all the fields I require, so that is not an issue. Your guidance gives me what I want to use said results. As we say here in the Southern Tip of Africa, Siyabonga Gakulu Mlungu. or Thank you muchly Boss. I will fiddle and run some tests till it does what I want it to. Quote Link to comment https://forums.phpfreaks.com/topic/312197-for-loops-learning/#findComment-1584708 Share on other sites More sharing options...
guymclarenza Posted February 23, 2021 Author Share Posted February 23, 2021 Should I keep this thread open if I have more questions? or should I start a new thread for my next issues? Quote Link to comment https://forums.phpfreaks.com/topic/312197-for-loops-learning/#findComment-1584709 Share on other sites More sharing options...
requinix Posted February 23, 2021 Share Posted February 23, 2021 1 hour ago, guymclarenza said: Should I keep this thread open if I have more questions? or should I start a new thread for my next issues? If the new question is related to what happened in this thread then go ahead and reuse it. Otherwise a new thread is nicer. Quote Link to comment https://forums.phpfreaks.com/topic/312197-for-loops-learning/#findComment-1584710 Share on other sites More sharing options...
guymclarenza Posted February 23, 2021 Author Share Posted February 23, 2021 Ta very mooch Quote Link to comment https://forums.phpfreaks.com/topic/312197-for-loops-learning/#findComment-1584721 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.