Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-24-2012, 11:11 PM   PM User | #1
phpchick
New Coder

 
Join Date: May 2011
Location: new york
Posts: 90
Thanks: 4
Thanked 0 Times in 0 Posts
phpchick is an unknown quantity at this point
Outputting individual of values to a new line in a text file

Array
(
[0] => stdClass Object
(
[symbol] => AAN
)

[1] => stdClass Object
(
[symbol] => AAP
)



So I have a file that runs this...

file_put_contents('stocksymbols.txt', print_r($stocksymbols, TRUE));

but it prints out the array in the format of the above. Is there a way to just do

AAPL
AAP
B
BAC

etc?
phpchick is offline   Reply With Quote
Old 12-25-2012, 01:17 AM   PM User | #2
tangoforce
Senior Coder

 
tangoforce's Avatar
 
Join Date: Feb 2011
Location: Your Monitor
Posts: 3,503
Thanks: 45
Thanked 439 Times in 428 Posts
tangoforce will become famous soon enoughtangoforce will become famous soon enough
Untested but should do the job:

PHP Code:
foreach ($stocksymbols as $Key => $Value)
   {
   
file_put_contents('stocksymbols.txt'$ValueFILE_APPEND);
   } 
__________________
Please wrap your code in [php] tags. It is a sticky topic and it HELPS us to HELP YOU!
TIP: Coding styles and $end errors :::::::::: TIP: Warning: Cannot modify header information - headers already sent :::::::::: TIP: Quotes / Parse error: syntax error, unexpected T_..
PHP Code:
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value. 
Explanation: The IE if(isset($_POST['submit'])) bug explained.
tangoforce is online now   Reply With Quote
Old 12-28-2012, 08:47 PM   PM User | #3
phpchick
New Coder

 
Join Date: May 2011
Location: new york
Posts: 90
Thanks: 4
Thanked 0 Times in 0 Posts
phpchick is an unknown quantity at this point
Now I have...

PHP Code:
$stocksymbols $wpdb->get_results"select meta_value as symbol from ii_postmeta
where meta_key = 'symbol' and meta_value not like '% %' and meta_value  not like ''
group by meta_value
order by meta_value asc"
); 


foreach (
$stocksymbols as $k=>$v)
{
file_put_contents('../stocks/stocksymbols.txt'$vFILE_APPEND);

but the file that results is blank, any ideas?
phpchick is offline   Reply With Quote
Old 12-28-2012, 08:58 PM   PM User | #4
tangoforce
Senior Coder

 
tangoforce's Avatar
 
Join Date: Feb 2011
Location: Your Monitor
Posts: 3,503
Thanks: 45
Thanked 439 Times in 428 Posts
tangoforce will become famous soon enoughtangoforce will become famous soon enough
What happens when you do this after the query?:

var_dump($stocksymbols);
__________________
Please wrap your code in [php] tags. It is a sticky topic and it HELPS us to HELP YOU!
TIP: Coding styles and $end errors :::::::::: TIP: Warning: Cannot modify header information - headers already sent :::::::::: TIP: Quotes / Parse error: syntax error, unexpected T_..
PHP Code:
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value. 
Explanation: The IE if(isset($_POST['submit'])) bug explained.
tangoforce is online now   Reply With Quote
Old 12-28-2012, 09:12 PM   PM User | #5
phpchick
New Coder

 
Join Date: May 2011
Location: new york
Posts: 90
Thanks: 4
Thanked 0 Times in 0 Posts
phpchick is an unknown quantity at this point
catching the tail end but the output looks like this


[1151]=>
object(stdClass)#1436 (1) {
["symbol"]=>
string(3) "ZLC"
}
[1152]=>
object(stdClass)#1437 (1) {
["symbol"]=>
string(3) "ZMH"
}
[1153]=>
object(stdClass)#1438 (1) {
["symbol"]=>
string(4) "ZNGA"
}
}
phpchick is offline   Reply With Quote
Old 12-28-2012, 09:43 PM   PM User | #6
phpchick
New Coder

 
Join Date: May 2011
Location: new york
Posts: 90
Thanks: 4
Thanked 0 Times in 0 Posts
phpchick is an unknown quantity at this point
seems like something fundamental is missing or wrong. I tried this as well...

PHP Code:
$stocksymbols $wpdb->get_results"select meta_value as symbol from ii_postmeta
where meta_key = 'symbol' and meta_value not like '% %' and meta_value  not like ''  and meta_value not like 'Multi'
group by meta_value
order by meta_value asc"
); 

file_put_contents('stocksymbols.txt'print_r($stocksymbolsTRUE));

$fp=fopen("test.txt","w+");

foreach (
$stocksymbols as $k=>$v){
fwrite($fp,$v);

the text.txt that gets written inside the foreach is blank, but stocksymbols.txt that gets written (the one outside of the foreach) outputs as


Array
(
[0] => stdClass Object
(
[symbol] => AAN
)

[1] => stdClass Object
(
[symbol] => AAP
)

[2] => stdClass Object
(
[symbol] => AAPL
)


as before
phpchick is offline   Reply With Quote
Old 12-28-2012, 09:54 PM   PM User | #7
tangoforce
Senior Coder

 
tangoforce's Avatar
 
Join Date: Feb 2011
Location: Your Monitor
Posts: 3,503
Thanks: 45
Thanked 439 Times in 428 Posts
tangoforce will become famous soon enoughtangoforce will become famous soon enough
Ok try using this:
PHP Code:
foreach ($stocksymbols as $Key => $Value)
   {
   
file_put_contents('stocksymbols.txt'$Value['symbol'], FILE_APPEND);
   } 
Note the symbol part on $Value which I forgot about last time. It might still not work but just try it.

Edit: As per FouLu's post use ->symbol instead of ['symbol'] (sorry I don't do oop so didn't think of that!)
__________________
Please wrap your code in [php] tags. It is a sticky topic and it HELPS us to HELP YOU!
TIP: Coding styles and $end errors :::::::::: TIP: Warning: Cannot modify header information - headers already sent :::::::::: TIP: Quotes / Parse error: syntax error, unexpected T_..
PHP Code:
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value. 
Explanation: The IE if(isset($_POST['submit'])) bug explained.

Last edited by tangoforce; 12-28-2012 at 10:05 PM..
tangoforce is online now   Reply With Quote
Old 12-28-2012, 10:03 PM   PM User | #8
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
stdclass cannot be output directly to a string.
In the file_put_contents, use $v->symbol instead of just $v.
Using a print_r on that example you have here should still write the output as it would with any print_r(x, true) call. That would indicate to me that you may be looking at the wrong file (you have used 2 different filepaths here). It is also possible that your function call includes an empty slot for the array, and since the file_put_contents you have here doesn't use append it will overwrite it every time. You'd be better off using var_dump instead of print_r since you could tell if its empty or null or what it is.

Needless to say, use the $v->symbol instead of $v in the put or write calls.
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
tangoforce (12-28-2012)
Old 01-02-2013, 05:30 PM   PM User | #9
phpchick
New Coder

 
Join Date: May 2011
Location: new york
Posts: 90
Thanks: 4
Thanked 0 Times in 0 Posts
phpchick is an unknown quantity at this point
So now I have

PHP Code:
foreach ($stocksymbols as $Key => symbol)
   {
   
file_put_contents('stocksymbols.txt'$Value['symbol'], FILE_APPEND);
   } 


But I'm getting an internal server error and the script stops completely.

In the error log I'm getting

[02-Jan-2013 10:25:26] PHP Parse error: syntax error, unexpected ')', expecting T_PAAMAYIM_NEKUDOTAYIM in /home5/instiuc5/jobs/stockcoverage.php on line 24

Line 24 is the line with the foreach statement.

I google that nekudotayim error and it has to do with missing braces it looks like?
But I don't believe I have any missing braces.
phpchick is offline   Reply With Quote
Old 01-02-2013, 05:42 PM   PM User | #10
tangoforce
Senior Coder

 
tangoforce's Avatar
 
Join Date: Feb 2011
Location: Your Monitor
Posts: 3,503
Thanks: 45
Thanked 439 Times in 428 Posts
tangoforce will become famous soon enoughtangoforce will become famous soon enough
Thats because you're doing two things wrong:

1) Leaving it days at a time before coming back
2) Ignoring things that people say.

Scroll up. I gave you a reply, then Fou-Lu replied correcting me. I went back and edited my post to tell you to ignore my reply and go with Fou-Lu's. Despite that, you've come back days later and still gone with my old and wrong advice, completely ignored my edit and completely ignored Fou-Lu.

If you're going to ignore people like that then what do you expect?
__________________
Please wrap your code in [php] tags. It is a sticky topic and it HELPS us to HELP YOU!
TIP: Coding styles and $end errors :::::::::: TIP: Warning: Cannot modify header information - headers already sent :::::::::: TIP: Quotes / Parse error: syntax error, unexpected T_..
PHP Code:
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value. 
Explanation: The IE if(isset($_POST['submit'])) bug explained.
tangoforce is online now   Reply With Quote
Old 01-02-2013, 05:43 PM   PM User | #11
phpchick
New Coder

 
Join Date: May 2011
Location: new york
Posts: 90
Thanks: 4
Thanked 0 Times in 0 Posts
phpchick is an unknown quantity at this point
the above problem looks like it was due to the "=", i changed it to -> and it was able to run, however the resulting file is still blank I do either


foreach ($stocksymbols as $Key -> symbol)
{
file_put_contents('stocksymbols.txt', symbol, FILE_APPEND);
}

or

foreach ($stocksymbols as $Key -> symbol)
{
file_put_contents('stocksymbols.txt', $Value['symbol'], FILE_APPEND);
}
phpchick is offline   Reply With Quote
Old 01-02-2013, 05:45 PM   PM User | #12
phpchick
New Coder

 
Join Date: May 2011
Location: new york
Posts: 90
Thanks: 4
Thanked 0 Times in 0 Posts
phpchick is an unknown quantity at this point
Quote:
Originally Posted by tangoforce View Post

If you're going to ignore people like that then what do you expect?

You said to use F-lou, if you look at the code I pasted, you can see that I have replaced your ['symbol'] with ->symbol

So I'm not sure why you are upset.
phpchick is offline   Reply With Quote
Old 01-02-2013, 05:47 PM   PM User | #13
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,635
Thanks: 4
Thanked 2,448 Times in 2,417 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
No paamayim nekudotayim is for scope resolution. You have a class named symbol, so you cannot use it in the foreach as you have. That would require the variable $symbol (or a public static property in the symbol class), and the $Value['symbol'] would be replaced by $symbol->symbol.
Fou-Lu is offline   Reply With Quote
Old 01-02-2013, 05:47 PM   PM User | #14
tangoforce
Senior Coder

 
tangoforce's Avatar
 
Join Date: Feb 2011
Location: Your Monitor
Posts: 3,503
Thanks: 45
Thanked 439 Times in 428 Posts
tangoforce will become famous soon enoughtangoforce will become famous soon enough
Re read what Fou said.

Look at the code I once gave you. Now read what Fou said. This is what he meant:

PHP Code:
foreach ($stocksymbols as $Key => $Value)
   {
   
file_put_contents('stocksymbols.txt'$Value->symbolFILE_APPEND);
   } 
Note the -> is inside the loop. Fou was quite clear about stating this must be changed in the file_put_contents() line of code. I also put in my edit (you know, the red box in my reply that says edit) that you must change ['symbol'] to ->symbol. Why did you then decide to change => to -> on the foreach line instead?
__________________
Please wrap your code in [php] tags. It is a sticky topic and it HELPS us to HELP YOU!
TIP: Coding styles and $end errors :::::::::: TIP: Warning: Cannot modify header information - headers already sent :::::::::: TIP: Quotes / Parse error: syntax error, unexpected T_..
PHP Code:
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value. 
Explanation: The IE if(isset($_POST['submit'])) bug explained.
tangoforce is online now   Reply With Quote
Old 01-02-2013, 06:00 PM   PM User | #15
phpchick
New Coder

 
Join Date: May 2011
Location: new york
Posts: 90
Thanks: 4
Thanked 0 Times in 0 Posts
phpchick is an unknown quantity at this point
Okay, there was some miscommunication in my reading of fou-lu's original correction. I had misread.

the script is working now with this:

$fp=fopen("test.txt","w+");

foreach ($stocksymbols as $k=>$v){
fwrite($fp,$v->symbol);
}

Apologize for the miscommunication, I changed the "=" to "-" due to my own error in trying to diagnose the problem.

apologies
phpchick is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:10 PM.


Advertisement
Log in to turn off these ads.