Jump to content

for loops, learning


guymclarenza
Go to solution Solved by requinix,

Recommended Posts

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 by guymclarenza
Link to comment
Share on other sites

  • guymclarenza changed the title to for loops, learning

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.

Link to comment
Share on other sites

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. 

Link to comment
Share on other sites

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 by guymclarenza
Link to comment
Share on other sites

  • Solution
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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.