Jump to content

printf issues?


Ruzzas

Recommended Posts

Hello, Well let me explain my problem to you guys.

 

I am using a printf function with an array, a custom function which i got off php.net

 

Anyways, Everytime I use it, It seems to leave random numbers at the end of the string for no reason at all.

 

Here is the result in source:

<td><a href="?page=download&name=DarkRP">DarkRP</a></td> 
      <td>08:10:17 AM 07/08/2010</td> 
      <td>Updated DarkRP from revision: 670 to revision: 67150</td> 
      Array
(
    [Name] => DarkRP
    [TimeStamp] => 1281186617
    [Revision1] => 670
    [Revision2] => 671
)
    </tr>

It is mean't to show

Updated DarkRP from revision: 670 to revision 671

but instead it has the random number 50 at the end for no reason...

 

Anyways heres the echoing code:

 

<?php
foreach($Logs as $Log){
$Time = TimeToDate($Log['TimeStamp']);
if($Log['Revision1']&$Log['Revision2']){
     $Message = 'Updated %1$s from revision: %3$s to revision: %4$d';
}else{
     $Message = 'Freshly downloaded: %1$s revision: %3$d';
}
?>
    <tr>
      <td><?php echo '<a href="?page=download&name='. $Log['Name'] .'">'. $Log['Name'] .'</a>'; ?></td>
      <td><?php echo $Time['Time'] .' '. $Time['Date']; ?></td>
      <td><?php echo printf_array($Message, $Log); ?></td>
      <?php print_r($Log); ?>
    </tr>
<?php
    }
?>
  </table>
  <br />

 

Functions:

 

function MySQLGetLogs(){
    global $Connection;
    if($Connection){
        $Query = sprintf('SELECT name, timestamp, revision1, revision2 FROM update_logs ORDER BY timestamp DESC;');
        if($Result = mysql_query($Query)){
            $Array = array();
            while($Row = mysql_fetch_array($Result)){
            $Array2 = array(array(
                                  'Name' => $Row['name'],
                                  'TimeStamp' => $Row['timestamp'],
                                  'Revision1' => $Row['revision1'],
                                  'Revision2' => $Row['revision2']
                                 )
                           );
                $Array = array_merge($Array, $Array2);
            }
            mysql_free_result($Result);
            return $Array;
        }
    }
}

function printf_array($Format, $Array) { 
    return call_user_func_array('printf', array_merge((array)$Format, $Array)); 
}

 

Also, I got the same issue with printf alone instead of the custom array function above.

Link to comment
https://forums.phpfreaks.com/topic/210068-printf-issues/
Share on other sites

You're using a bitwise comparison operator & here

if($Log['Revision1']&$Log['Revision2']){

I think you meant to use &&

 

Yeah... Didn't make no difference, Whatsoever

 

Also:

 

      <td>Freshly downloaded: PewPew revision: 038</td> 
      Array
(
    [Name] => PewPew
    [TimeStamp] => 1280744518
    [Revision1] => 0
    [Revision2] => 376
)
    </tr> 

 

if($Log['Revision1']&&$Log['Revision2']){
     $Message = 'Updated %1$s from revision: %3$s to revision: %4$d';
}else{
     $Message = 'Freshly downloaded: %1$s revision: %3$d';
}

Link to comment
https://forums.phpfreaks.com/topic/210068-printf-issues/#findComment-1096324
Share on other sites

Well you helped me with my problem so I will toss a few ideas at you.

 

Have you tried print_r, just to see if the numbers are inside any of the array elements?

 

Also array_merge will merge strings of the same sequence, I think, but not numbers, could this be causing extra numbers to be appended?

 

Also maybe print one thing out at a time to see where the 50 is coming from?

 

Sorry I am not much help just tossing some ideas out.

Link to comment
https://forums.phpfreaks.com/topic/210068-printf-issues/#findComment-1096330
Share on other sites

Well you helped me with my problem so I will toss a few ideas at you.

 

Have you tried print_r, just to see if the numbers are inside any of the array elements?

 

Also array_merge will merge strings of the same sequence, I think, but not numbers, could this be causing extra numbers to be appended?

 

Also maybe print one thing out at a time to see where the 50 is coming from?

 

Sorry I am not much help just tossing some ideas out.

 

If you read the code:

<?php print_r($Log); ?>

Link to comment
https://forums.phpfreaks.com/topic/210068-printf-issues/#findComment-1096333
Share on other sites

Have you tried changing to something like this? Just to see of course.

 

if($Log['Revision1']&$Log['Revision2']){
     $Message = 'Updated $Log[Name] from revision: $Log[Revision1] to revision: $Log[Revision2]';
}else{
     $Message = 'Freshly downloaded: $Log[Name] revision: $Log[Revision1]';
}

 

Also try putting a space at the end of $Message. Like so:

 

$Message = 'Updated $Log[Name] from revision: $Log[Revision1] to revision: $Log[Revision2] ';

 

That way you will know if the random numbers are part of the Revision number or are appended to the string.

Link to comment
https://forums.phpfreaks.com/topic/210068-printf-issues/#findComment-1096337
Share on other sites

Have you tried changing to something like this? Just to see of course.

 

if($Log['Revision1']&$Log['Revision2']){
     $Message = 'Updated $Log[Name] from revision: $Log[Revision1] to revision: $Log[Revision2]';
}else{
     $Message = 'Freshly downloaded: $Log[Name] revision: $Log[Revision1]';
}

 

Also try putting a space at the end of $Message. Like so:

 

$Message = 'Updated $Log[Name] from revision: $Log[Revision1] to revision: $Log[Revision2] ';

 

That way you will know if the random numbers are part of the Revision number or are appended to the string.

 

It's a printf problem, Without it, it seems to work fine.

But I want it with printf so I can chuck it in my settings for easy configuration.

Link to comment
https://forums.phpfreaks.com/topic/210068-printf-issues/#findComment-1096341
Share on other sites

In your printf_array function make it call sprintf

function printf_array($Format, $Array) { 
    return call_user_func_array('sprintf', array_merge((array)$Format, $Array)); 
}

 

printf will return the length of the outputted string. sprintf will return the formatted string

Link to comment
https://forums.phpfreaks.com/topic/210068-printf-issues/#findComment-1096342
Share on other sites

In your printf_array function make it call sprintf

function printf_array($Format, $Array) { 
    return call_user_func_array('sprintf', array_merge((array)$Format, $Array)); 
}

 

printf will return the length of the outputted string. sprintf will return the formatted string

 

I did not know this, Thank you Mr wildteen88.

Link to comment
https://forums.phpfreaks.com/topic/210068-printf-issues/#findComment-1096349
Share on other sites

In your printf_array function make it call sprintf

function printf_array($Format, $Array) { 
    return call_user_func_array('sprintf', array_merge((array)$Format, $Array)); 
}

 

printf will return the length of the outputted string. sprintf will return the formatted string

 

I did not know this, Thank you Mr wildteen88.

I just learnt that from reading the manual, ha :o

Link to comment
https://forums.phpfreaks.com/topic/210068-printf-issues/#findComment-1096351
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.