Count records and display

Warning: Invalid argument supplied for foreach() in … 294


		foreach($arrVenues as $arrVenue)  // line294

As in the code above, whats causing this?

actually this code
$arrVenues = (“SELECT category_id, venue_id FROM tbl_venues ORDER BY category_id”, ARRAY_A);
does assign a string to $arrVenues variable

Thanks Shrapnel, so what are you saying? Do I need to remove something?

To add I’d say.
Do you know what to do with SQL queries? Do you have any in your scripts?
Why not to do the same with this one?

Do you know what to do with SQL queries? Do you have any in your scripts?
Isn’t that what we’ve been doing for the past 2 nights? I’m familiar with SQL, are you saying revert back to the original method: keep our new select statement and output the results the same as all the other scripts?

output example from previous script:

<? if($num_rows4): ?>
			<ul>
		<? foreach($DATA3 as $row4): ?>
				
				<li></li>
		
		<? endforeach ?>
			</ul>
		<? else: ?>
		 <p>No data found</p>
		<? endif ?>

Correct or am I missing something here Shrapnel? :cool:

You have an SQL query in the $arrVenues variable.
This query needs to be assigned to another variable. Call it $sql
Then executed.
And results assigned to $arrVenues array.

Now you can use this variable in this code

Ok Shrapnel, getting a bit confusing now

So are you saying to use the old method and scrap oddz code?

something like:

$sql5 =("SELECT category_id, venue_id FROM tbl_venues ORDER BY category_id", ARRAY_A); 
			$result5 = mysql_query($sql5) or die(mysql_error());
			$DATA5=array();
			while ($arrVenues = mysql_fetch_array($result5, MYSQL_ASSOC)) {

then use the output above ??? :cool:

Not exactly.
Write $arrVenues instead of $DATA5 here
and then fill it with data, as you do in the other scripts

This is the good standard way of doing the above code:


$data = array();
$sql = 'SELECT category_id, venue_id FROM tbl_venues ORDER BY category_id';
$result = mysql_query($sql) or die(mysql_error());
while ($row = mysql_fetch_assoc($result)) {
    array_push($data, $row);
}

:slight_smile: it works, I was Expecting some errors, just a couple of tweaks :cool:

Ok guys the full code

select

$sql5 = "SELECT category_id, venue_id FROM tbl_venues ORDER BY category_id"; 
			$result5 = mysql_query($sql5) or die(mysql_error());
			$arrVenues =array();
			while ($row5 = mysql_fetch_array($result5, MYSQL_ASSOC)) {
			$arrVenues[]=$row5;
			}
			$num_rows5=mysql_num_rows($result5);

output

<!-- list venue categories -->
		<? 
		$intCategory = null;
		foreach($arrVenues as $arrVenue) {
 
		if($intCategory != $arrVenue['category_id']) {
			if($intCategory !== null) echo '</ul></div>';
			echo '<div><h2>',$arrVenue['category_id'],'</h2><ul>';
			$intCategory = $arrVenue['category_id'];
		}
 
			echo '<li>',$arrVenue['venue_id'],'</li>';
		}
		?>

Thanks pmw57. If your way is more of a standard then I’ll have to change a few things, 5 SELECT query’s to be exact, but I’d like to get my code as good as it can be, what are the benefits of doing it your way?

Thanks again Shrapnel :). Just wondering can we change this output so it keeps inline with the others, without the echo’s?

One benefit is that you can easily copy/paste from one place to another, but that’r eally should be done is we’re concerned with DRY (Don’t Repeat Yourself) so you can place the code in a function called queryAll, so that you can then feed it a SQL string and get back the data.

If your server already has PEAR installed on it, you can make use of the MDB2 library which allows you to do:


$data = $mdb2->queryAll('SELECT SomeFields FROM SomeTable WHERE SomeClause');

But even without it, you can achieve a similar approximation and prevent large amounts of database code from being mixed in throughout the rest of your code.

I see Paul thanks, I’ll look more at this when I get what I have working exactly how I want it, spent a lot of time past two days with this so… :slight_smile: thanks for pointing this out, it can be a pain repeating similar code over and over, I know what you mean cheers :cool:

Just wondering as well, is it quite common to have 5+ SELECT statements running at the same time with different bits of code on the same page?

update

I have this working like the other snippets Shrapnel but, it’s adding a category_id for every venue, instead one category_id for a group of venues. What do I need to add from oddz snippet, if you have time cheers :)?

<? if($num_rows5): ?>
			
		<? foreach($arrVenues as $row5): ?>
				<ul>
				<h2><?=$row5['category_id']?></h2>
				<li><a href="/<?=$row5['category_id']?>"><?=$row5['venue_id']?></a></li>
				</ul>
		<? endforeach ?>
		<? else: ?>
		 <p>No data found</p>
		<? endif ?>

ust wondering as well, is it quite common to have 5+ SELECT statements running at the same time with different bits of code on the same page?

A programmer would make a function of this code. Just to shrink the code itself.
As you not a programmer, yet, it is ok to leave these bits as is.

I need to add from oddz snippet, if you have time

I have time, but sorry, I don’t like to waste it.

A programmer would make a function of this code. Just to shrink the code itself.
Yes I understand but each SELECT’s will be making different query’s, so I didn’t see how you could use one function to cater for all, I’ll do some research thinking maybe stored procedures might be a good solution, thanks.

As you not a programmer, yet, it is ok to leave these bits as is.
I’m a front-end developer Shrapnel, still learning PHP and other back-end languages, thanks for reminding me :slight_smile:

I have time, but sorry, I don’t like to waste it.
A don’t see how your wasting your time Shrapnel, I’ve had a problem and you’ve been helping me (I’m learning), isn’t that what these forums are for? Like I said before, I appreciate the help greatly, maybe I just don’t understand things as easy as you do. :cool:

I didn’t see how you could use one function to cater for all

functions has arguments
you can pass your queries as an arguments to the function.

A don’t see how your wasting your time Shrapnel, I’ve had a problem and you’ve been helping me (I’m learning)

I am sorry to mention it, but I can’t see you learn a little bit.

You have a goal very similar to ones you accomplished many times before: to have an array from the sql query. You even noticed that all these code bits are very similar. but you failed it. As little thing as different variable name spoiled you.

Say, you have a query,
$sql3 = “SELECT venue_id FROM tbl_venues”;
and want to have an array
$DATA1
so, you’re using the code

    $sql3 = "SELECT venue_id FROM tbl_venues";
    $result3 = mysql_query($sql3) or die(mysql_error());
    $DATA1=array();
        while ($row3 = mysql_fetch_array($result3, MYSQL_ASSOC)) {
        $row3['title']=str_replace("-"," ",(ucwords($row3['venue_id'])));
        $row3['url']=str_replace(" ","-",(strtolower(ucwords($row3['venue_id']))));
        $DATA1[]=$row3;
        }

and get it.

Now you have very similar conditions: a query
$sql5 = “SELECT category_id, venue_id FROM tbl_venues ORDER BY category_id”;
and want to have an array
$arrVenues
…and then fail
I can’t believe it

Same for the HTML part.
It consists of very few operators. Very easy to learn.
But you never learn, you just copy.

I am sorry to mention it, but I can’t see you learn a little bit.
I’ve learned a lot Shrapnel, good use of foreach(), better and cleaner templates amongst other things…

But you never learn, you just copy.
Maybe a bit, you know how it is when you want to get something working :slight_smile:

I understand what your saying and I understand what we’ve been working on the past couple of nights, it’s just when I’m trying to separate the venues into the different category_id’s… my code does work just in the wrong way.

example:

bars
venue 1
bars
venue 2
clubs
venue 3
clubs
venue 4

it should be

bars
venue 1
venue 2
clubs
venue 3

I was having trouble understand what to take from oddz code, so we can get it to work without using echo, cleaning templates :cool:

This is what I had.
outputs what I want
bars
venue 1
venue 2

<?
        $intCategory = null;
        foreach($arrVenues as $arrVenue) {
 
        if($intCategory != $arrVenue['category_id']) {
            if($intCategory !== null) echo '</ul></div>';
            echo '<div><h2>',$arrVenue['category_id'],'</h2><ul>';
            $intCategory = $arrVenue['category_id'];
        }
 
            echo '<li>',$arrVenue['venue_id'],'</li>';
        }
        ?>

but know replaced with (cleaner code)
outputs
bars
venue 1
bars
venue 2

<? if($num_rows5): ?>
           
        <? foreach($arrVenues as $row5): ?>
                <ul>
                <h2><?=$row5['category_id']?></h2>
                <li><a href="/<?=$row5['category_id']?>"><?=$row5['venue_id']?></a></li>
                </ul>
        <? endforeach ?>
        <? else: ?>
         <p>No data found</p>
        <? endif ?>

Can you see what I mean? :slight_smile:
I need to fit something like…

($intCategory != $arrVenue[‘category_id’]) {
if($intCategory !== null)

…into this new code so it only creates the new category_id after its shown all the venues from that category, like the first example.

You have syntax of all these operators
As you can see from the code you posted above,

 if(confition) { echo "text"; }

can be replaced with

<? if (condition): ?>text<? endif ?>

hope you got the idea

hope you got the idea
Yes I got the idea… cracking up, been trying for the past hour, just not sure what goes where?

What I have:

<? if($num_rows5): ?>
			
		<? foreach($arrVenues as $row5): ?>
		<? if($intCategory != $arrVenues['category_id']) 
			if ($intCategory !== null): ?>
			</ul><h2>
			<?=$row5['category_id']?>
			</h2><ul>
		
		<? $intCategory = $arrVenues['category_id'];  ?>
		
		<? endif ?>

			<li><a href="/<?=$row5['venue_id']?>"><?=$row5['venue_id']?></a></li>
				
		<? endforeach ?>

It outputs

bars
venue 1
venue 2
venue 3 ( this is a club venue)

it should be
clubs
venue 3

Something simple? :cool:

you have two if statements here
both must have <? if (condition): ?>text<? endif ?> format