Small issue trying to print array values inside for loop [Solved]

Hi all

I’ve become a little stuck after I changed some of my code. Can’t figure out what I have changed because the coma’s and double quotes are causing some errors in the for loop.

When I load the page all I can see is:

ARRAY[‘MENU_NAME’] ARRAY[‘MENU_NAME’] ARRAY[‘MENU_NAME’]

How do I properly print the for loop so it displays the values correctly?

$stmtt = $mysqli->prepare("SELECT menu_name, menu_link, menu_class FROM menu");
$stmtt->bind_result($menu_name, $menu_link, $menu_class);
$stmtt->execute();
$stmtt->store_result();

$lnks = array();
while($stmtt->fetch()){
    $lnks[] = array(
        'menu_name' => $menu_name,
        'menu_link' => $menu_link,
        'menu_class' => $menu_class
    );
}

print "<nav>";
    print "<div>";
      print "<ul>";
        for($i = 0; $i < count($lnks); $i++):
            print "<li class='$lnks[$i]['menu_class']'><a href='/$lnks[$i]['menu_link']' class='$lnks[$i]['menu_class']'>$lnks[$i]['menu_name']</a></li>";
        endfor;
      print "</ul>";
    print "</div>";
print "</nav>";

Thanks,
Barry

Where are the values magically coming from in the lnks array. It would seem you have twice as many loops as you need. What is with the print overkill?

This is a perfect case for you to start using version control and committing often.

Thanks @benanamen

I’ve updated my code in the first post.

Regarding the print overload, its something I’m testing. If i print it this way I remove all spaces and newlines when I render to the page. Small SEO boost. Just playing for now as some functions where causing issues with other code.

Barry

Whenever I get stuck, I try to refactor the code to make it more readable. Try something like this:

$name = $lnks[$i]['menu_name'];
$class = $lnks[$i]['menu_class'];
$link = $lnks[$i]['menu_link'];

echo "<li class='$class'><a href='$link'>$name</a></li>";

Here’s the full code:

echo "<nav><div><ul>";

for($i = 0; $i < count($lnks); $i++):
  $name = $lnks[$i]['menu_name'];
  $class = $lnks[$i]['menu_class'];
  $link = $lnks[$i]['menu_link'];
  echo "<li class='$class'><a href='$link'>$name</a></li>";
endfor;

echo "</ul></div></nav>";

And here’s a demo: http://sandbox.onlinephpfunctions.com/code/7ec9b4a6ab89821550285b3c399335f890bf4bfc

1 Like

Where do you get the idea that is an SEO boost? All you get is a very unreadable source. I would highly recommend you you use PDO. Here is a tutorial to get you going.

@tensormonkey, nice job :+1:

1 Like

Perfect :smile:
Thanks @tensormonkey

I see what you’ve done there, nice reminder. I’ve recently been doing something similar refactoring a bit of javascript, same principles apply.

Its working great now.

And thanks for the link @benanamen - I’ve been meaning to look into this :nerd_face:

Where do you get the idea that is an SEO boost?

Its well known in the SEO circles. If you have thousands of lines of html code which can be compressed on rendering then its a win win as far as the browsers are concerned.

Thanks, Barry

First you said it is an SEO boost, now you said it is a win win for the browsers.

While it may be “well known in the SEO circles” can you cite any source that shows it matters to google or any other search engine? If this is true I want to factually know about it so I don’t have to say @computerbarry said so.

I’m guessing you are referring to code “minification”, for the purpose of a speed boost.

It’s not something I have used, my take being that the gains are negligible weighed against the hoops you will jump through.
Though in the case where you do save a significant quantity of bytes by it, you must have a serious bloat problem for that to be the case.

1 Like

I’m not one for believing SEO snake oil, in fact I usually write code so it will add newlines and tabs to the generated HTML so it’s easier to read in view-source.

1 Like

I’m guessing you are referring to code minification, for the purpose of a speed boost.

Correct @SamA74 , this was the main purpose really.

you must have a serious bloat problem for that to be the case.

:grinning: I’m just testing to see how things go.

Like with any javascript or css file its best practice to minify them for production, the same principle applies for HTML.

I usually write code so it will add newlines and tabs to the generated HTML so it’s easier to read in view-source.

Yes I agree @Mittineague, though this has no effect when you inspect the code everything is still formatted for easy viewing. Again, just testing.

can you cite any source that shows it matters to google or any other search engine

Not of the top of my head @benanamen its information and knowledge I’ve gathered over the years, been in many discussions and read many articles about minifying code and speeding up the page for the first paint.

Anyhow, cheers all getting a little side tracked now :nerd_face:

For anybody interested
Minify code for page speed

Barry

I would say ok on the JS and CSS as I almost never need to look at it, but the HTML source, all the time.

Sure it does. It is one single line of code. What browser are you using that will show it formatted? EDIT: OK, I kinda take it back. If you use the Developers tools it will be formatted. View Source will not be. I want the option to see what the developer actually did without it being manipulated by dev tools.

For completeness, if you really feel the need to compress your HTML just enable GZIP compression on the server instead of messing with source code gymnastics. But as mentioned, if you need it you probably have a design problem.

As it’s marked as “Solved” I guess it’s OK to go off an a tangent, insn’t it? :shifty:

I believe it is documented that speed has become a ranking factor, but how big a factor is another question, and how big the gains in speed from minification yet another.
Again, to make a significant difference, you need a lot of bloat to start with, so you are already on a loser if that’s the case.

I forget, bloat is the norm these days. :disappointed:

1 Like

You should minify your HTML, CSS, and JavaScript

Straight from google in the link I posted :nerd_face:

Sure it does. It is one single line of code. What browser are you using that will show it formatted?

Right click and inspect - not view source.

For completeness, if you really feel the need to compress your HTML just enable GZIP compression on the server instead of messing with source code gymnastics

Extra benefit if you do both.

I guess it’s OK to go off an a tangent, insn’t it?

:grin: feel free! Ha

I believe it is documented that speed has become a ranking factor, but how big a factor is another question, and how big the gains in speed from minification yet another.

Every little helps :smiley:

Aw c’mon @computerbarry, if your going to make a claim post the supporting link. Are you really going to make me go look for it, lol? I have not been in the SEO game for many years so I am not up on it. My gig is backend and you best not be trying to SEO my backend. :rofl:

1 Like

All good fun. Nice you can see the funny side :grin:

Anyhow, here is the link again - google page speed reference

These sort of discussions are how we learn @benanamen :nerd_face:

Barry

Initially I was using the below function, though this was causing errors when using inline javascript and CSS.

function minify_output($buffer)
{
	$search = array(
        '/\>[^\S]+/s',     	// strip whitespaces after tags
        '/[^\S ]+\</s',     // strip whitespaces before tags, except space
        '/(\s)+/s',         // shorten multiple whitespace sequences
        '/<!--(.|\s)*?-->/' // remove HTML comments
    );
	$replace = array(
		'>',
		'<',
		'\\1',
		''
	);
	if (preg_match("/\<html/i",$buffer) == 1 && preg_match("/\<\/html\>/i",$buffer) == 1) {
		$buffer = preg_replace($search, $replace, $buffer);
	}
	return $buffer;
}
ob_start("minify_output");

Thanks again all!

It depends of course, but I can’t imagine the speed difference between readable vs compressed HTML being more than an insignificant 0.0045ms or thereabouts. In my limited experience where the truly significant speed optimizations can be made are with unoptimized images.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.