Page 1 of 2

[SOLVED] Printf mistake ?

Posted: Tue Dec 28, 2004 12:04 pm
by pookie62
Hi,
I was playing around with printf and tried this:

Code: Select all

<?php
$format = 'Results for %1 held %1 at %3';
printf($format,$w-naam,$date,$location);
?>
Just gave me output like this :
Results for held at
What is wrong with this code?

Posted: Tue Dec 28, 2004 12:16 pm
by feyd
the control values in [php_man]printf()[/php_man] are for type casting, they do not use numbers for anything other than alignment.

Posted: Tue Dec 28, 2004 12:20 pm
by pookie62
Hm oke, well I tried to echo this line as well
$w-naam,$date,$location);
but I couldn't get that either right..
..I know, I'm a real newbie.. sorry
How should I echo tthat line ?
Thanks for you quick reply !

Posted: Tue Dec 28, 2004 12:37 pm
by feyd
there's the echo way:

Code: Select all

echo "Results for $w_naam held $w_naam at $location";
in order to tell you what to use for printf, I'd need to know the types those variables are.

Posted: Tue Dec 28, 2004 12:43 pm
by pookie62
Thanks feyd !
These are two text fields ; $w_naam and $w_location
The other is date field: $w_date

Posted: Tue Dec 28, 2004 12:46 pm
by feyd

Code: Select all

printf('Results for %s held %s at %s', $w_naam, $w_date, $w_location);

Posted: Tue Dec 28, 2004 12:55 pm
by pookie62
Thanks for your help feyd, but both options only output the first $ ($w_naam) Other $ are not displayed at all.. ??

Posted: Tue Dec 28, 2004 1:03 pm
by feyd
make sure the variable names are correct.. turn on full error and warning displays and such...

Posted: Tue Dec 28, 2004 1:12 pm
by pookie62
Let me post more of my code, you'll probably see right away where the mistake is..

Code: Select all

<?php
while ($row = mysql_fetch_assoc($result))
{
   $wedstrijdnaam[] = $row['Naam'];
   $w_datum[] = $row['Datum'];
   $w_plaats[] = $row['plaats'];
}

/*Similarly for your other drop-down list boxes


$wedstrijdnaam[] = $wedstrijdnaam;
$wedstrijdnaam[] = 'Wedstrijd 2';
$wedstrijdnaam[] = 'Wedstrijd 3';

/*
$genre_names[] = 'Genre 1';
$genre_names[] = 'Genre 2';
$genre_names[] = 'Genre 3';
*/

if (true === isset($_POST['Search']))
{
    $where_clause = '';
    // handle search
    if (true === isset($_POST['Naam']))
    {
        // Check for search by Wedstrijd
        $w_naam = $_POST['Naam'];
		 if (0 < strlen($w_naam))
        {
          $where[] = ' Naam = ''' . $w_naam . '''';
        }

        /* check for search by genre
        $g_name = $_POST['genre_name'];
        if (0 < strlen($g_name))
        {
          $where[] = ' genre_name = ''' . $g_name . '''';
        }
		*/

        if (true === is_array($where))
        {
            $where_clause = implode(' AND ', $where);
        }
    }

    /*
        build your query to search the appropriate talbe(s)
        and add the $where_clasuse to it, something like */

//<center><b><strong><font size="6" color="#FFFFFF"><?php echo 'Uitslag van ' . $w_naam; </font></strong></b></center>
//echo "Results for $w_naam held $w_datum at $w_plaats";
printf('Results for %s held %s at %s', $w_naam, $w_datum, $w_plaats);

}
?>

Posted: Tue Dec 28, 2004 1:55 pm
by timvw
$w_datum and $w_plaats are arrays... and you are trying to pass them to a function that is not expecting arrays....

Posted: Tue Dec 28, 2004 2:06 pm
by pookie62
Oke, understand.. how can I solve this ?
I got a similair issue in another script, would be very happy to see an example how to do this.

Posted: Tue Dec 28, 2004 4:48 pm
by feyd
using any one of the looping controls and iterating through the array printing them out in the format you wish, or using some of the built-in array printing functions... ([php_man]print_r()[/php_man])

Posted: Wed Dec 29, 2004 2:56 am
by pookie62
How would I implement this in my code ?
I read the print_r article but it looks to me that the whole aray is echo-ed.
Could you please provide a few lines of code how to uset his ?
Thanks !

Posted: Wed Dec 29, 2004 3:21 am
by n00b Saibot
Straight from Manual...

Code: Select all

<pre>
<?php
    $a = array ('a' => 'apple', 'b' => 'banana', 'c' => array ('x', 'y', 'z'));
    print_r ($a);
?>
</pre>  

Which will output: 

<pre>
Array
(
    &#1111;a] => apple
    &#1111;b] => banana
    &#1111;c] => Array
        (
            &#1111;0] => x
            &#1111;1] => y
            &#1111;2] => z
        )
)
</pre>
I hope that helps u a bit...
Lemme know...

Bye 4 now!

Posted: Wed Dec 29, 2004 3:30 am
by pookie62
I saw this example, but still don't understand how to use it it the code that I posted.. I'm real newbie so...
How must I use in this line ??

Code: Select all

<?php
echo "Results for $w_naam held $w_naam at $location";
 
?>