Jump to content

simple question: why this don't work?


buothz
 Share

Recommended Posts

$dati_auto = array("auto_marca", "auto_tipo", "auto_prezzo");
$maxi = count($dati_auto);
echo 'max n vale' . $maxn;

for ($i = 0; $i < $maxi; $i++) {
	//echo '<p>dati auto vale ' . $dati_auto[$i].'</p>';
	$field = $fields->get('$dati_auto[$i]'); 
  	$label = $field->get("label$language");  
  	echo "<li><strong>$label:</strong> $page->$dati_auto[$i]</li>";

}

why this code don't work?

There's a method to take an array with my vars and put in a cycle? 

Thank u ;)

Link to comment
Share on other sites

This:

$field = $fields->get('$dati_auto[$i]'); 

Should be without single quotes, like this:

$field = $fields->get($dati_auto[$i]); 

Otherwise you are treating it as a string.

  • Like 1
Link to comment
Share on other sites

I'm not sure I understand what you're trying to achieve, but let's try and fix a couple of little mistakes from the code :).

echo 'max n vake' . $maxn;

There's no variable named $maxn, there should probably read $maxi.

$field = $fields->get('$dati_auto[$i]');

You're using single quotes so PHP treats what's between them as a literal string and does not interpolate the variable. As there's nothing more than a variable there, you can leave the single quotes off. To make PHP interpolate the variable, use double quotes as you've done at the very next line. The $language variable shown there is initialized to some meaningful value somewhere as well, right?

And yet another variable interpolation thing here:

echo "<li><strong>$label:</strong> $page->$dati_auto[$i]</li>";

Should be:

echo "<li><strong>$label:</strong> {$page->$dati_auto[$i]}</li>";

(Ah, Pete was faster, so no hurry anymore for me. I'll go on a little more then.)

And of course you'd need fields called auto_marca, auto_tipo and auto_prezzo to exists in the template of this page for this to work in the first place. But I think you've covered that already.

One more thing, and this is just a preference, not a mistake at all. I like to iterate over that kind of array like this:

foreach($dati_auto as $field_name) {    $field = $fields->get($field_name);    // ...    echo "... {$page->$field_name} ...";}

But as I said, there's nothing wrong with the way you've done it :).

(Edit: Ngh.. The editor messed the code totally - had to fix them afterwards. I wanted to see if the post looks alright before posting it and used full editor for that (more options or whatever the button says there). Then submitted the post and code was all over the place...)

Edited by nik
  • Like 1
Link to comment
Share on other sites

I'm not sure I understand what you're trying to achieve, but let's try and fix a couple of little mistakes from the code :).
echo 'max n vake' . $maxn;

There's no variable named $maxn, there should probably read $maxi.

$field = $fields->get('$dati_auto[$i]');

You're using single quotes so PHP treats what's between them as a literal string and does not interpolate the variable. As there's nothing more than a variable there, you can leave the single quotes off. To make PHP interpolate the variable, use double quotes as you've done at the very next line. The $language variable shown there is initialized to some meaningful value somewhere as well, right?

And yet another variable interpolation thing here:

echo "<li><strong>$label:</strong> $page->$dati_auto[$i]</li>";

Should be:

echo "<li><strong>$label:</strong> {$page->$dati_auto[$i]}</li>";

(Ah, Pete was faster, so no hurry anymore for me. I'll go on a little more then.)

And of course you'd need fields called auto_marca, auto_tipo and auto_prezzo to exists in the template of this page for this to work in the first place. But I think you've covered that already.

One more thing, and this is just a preference, not a mistake at all. I like to iterate over that kind of array like this:

foreach($dati_auto as $field_name) {
    $field = $fields->get($field_name);
    // ...
    echo "... {$page->$field_name} ...";
}

But as I said, there's nothing wrong with the way you've done it :).

(Edit: Ngh.. The editor messed the code totally - had to fix them afterwards. I wanted to see if the post looks alright before posting it and used full editor for that (more options or whatever the button says there). Then submitted the post and code was all over the place...)

thank u to all! Problem resolved!

Another little question: why i can't see name of field but only id???

why.jpg

Link to comment
Share on other sites

These look like its a user select. Ids are in the range of admin etc. You should not choor title but name as the label as users dont have a title field.

If i've understand well what do u tell -> ids are of the template name

for example 29 is basic-page.php template etc... 

I've the same problem for the fields, I can't see field's name but only id... :(

how can I resolve this problem?

 
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...