[SOLVED] Printf mistake ?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

pookie62
Forum Commoner
Posts: 92
Joined: Tue Dec 07, 2004 2:44 pm

[SOLVED] Printf mistake ?

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
pookie62
Forum Commoner
Posts: 92
Joined: Tue Dec 07, 2004 2:44 pm

Post 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 !
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
pookie62
Forum Commoner
Posts: 92
Joined: Tue Dec 07, 2004 2:44 pm

Post by pookie62 »

Thanks feyd !
These are two text fields ; $w_naam and $w_location
The other is date field: $w_date
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

printf('Results for %s held %s at %s', $w_naam, $w_date, $w_location);
pookie62
Forum Commoner
Posts: 92
Joined: Tue Dec 07, 2004 2:44 pm

Post by pookie62 »

Thanks for your help feyd, but both options only output the first $ ($w_naam) Other $ are not displayed at all.. ??
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

make sure the variable names are correct.. turn on full error and warning displays and such...
pookie62
Forum Commoner
Posts: 92
Joined: Tue Dec 07, 2004 2:44 pm

Post 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);

}
?>
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

$w_datum and $w_plaats are arrays... and you are trying to pass them to a function that is not expecting arrays....
pookie62
Forum Commoner
Posts: 92
Joined: Tue Dec 07, 2004 2:44 pm

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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])
pookie62
Forum Commoner
Posts: 92
Joined: Tue Dec 07, 2004 2:44 pm

Post 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 !
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post 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!
pookie62
Forum Commoner
Posts: 92
Joined: Tue Dec 07, 2004 2:44 pm

Post 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";
 
?>
Post Reply