473,396 Members | 1,935 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

comparing two strings, what am I doing wrong?

Hi,

I am working with the Facebook API right now, an I have kind of a
problem, but I don't know what I am doing wrong.

So I have a few arrays, f.ex.:

User albums:

array(2) {
[0]=>
array(10) {
["aid"]=>
string(19) "2322329421568036697"
["cover_pid"]=>
string(19) "2322329421568483956"
["owner"]=>
string(9) "540709454"
["name"]=>
string(5) "test2"
["created"]=>
string(10) "1212610115"
["modified"]=>
string(10) "1212610148"
["description"]=>
string(42) "Description 2 balaldlsaüdkopas jdkaj daj"
["location"]=>
string(9) "Location2"
["link"]=>
string(56) "http://www.facebook.com/album.php?
aid=20313&id=540709454"
["size"]=>
string(1) "6"
}
[1]=>
array(10) {
["aid"]=>
string(19) "2322329421568036696"
["cover_pid"]=>
string(19) "2322329421568483947"
["owner"]=>
string(9) "540709454"
["name"]=>
string(5) "test1"
["created"]=>
string(10) "1212609951"
["modified"]=>
string(10) "1212610028"
["description"]=>
string(23) "Description 1 blablabla"
["location"]=>
string(9) "Location1"
["link"]=>
string(56) "http://www.facebook.com/album.php?
aid=20312&id=540709454"
["size"]=>
string(1) "9"
}
}

User photos:

array(15) {
[0]=>
array(8) {
["pid"]=>
string(19) "2322329421568483956"
["aid"]=>
string(19) "2322329421568036697"
["src_small"]=>
string(95) "http://photos-e.ak.facebook.com/photos-ak-sf2p/
v258/189/57/540709454/t540709454_467572_7021.jpg"
["src_big"]=>
string(95) "http://photos-e.ak.facebook.com/photos-ak-sf2p/
v258/189/57/540709454/n540709454_467572_7021.jpg"
["src"]=>
string(95) "http://photos-e.ak.facebook.com/photos-ak-sf2p/
v258/189/57/540709454/s540709454_467572_7021.jpg"
["link"]=>
string(57) "http://www.facebook.com/photo.php?
pid=467572&id=540709454"
["caption"]=>
string(9) "caption21"
["created"]=>
string(10) "1212610147"
}
[1]=>
array(8) {
["pid"]=>
string(19) "2322329421568483957"
["aid"]=>
string(19) "2322329421568036697"
["src_small"]=>
string(95) "http://photos-f.ak.facebook.com/photos-ak-sf2p/
v258/189/57/540709454/t540709454_467573_7347.jpg"
["src_big"]=>
string(95) "http://photos-f.ak.facebook.com/photos-ak-sf2p/
v258/189/57/540709454/n540709454_467573_7347.jpg"
["src"]=>
string(95) "http://photos-f.ak.facebook.com/photos-ak-sf2p/
v258/189/57/540709454/s540709454_467573_7347.jpg"
["link"]=>
string(57) "http://www.facebook.com/photo.php?
pid=467573&id=540709454"
["caption"]=>
string(9) "caption22"
["created"]=>
string(10) "1212610147"
}
[2]=>
array(8) {
["pid"]=>
string(19) "2322329421568483958"
["aid"]=>
string(19) "2322329421568036697"
["src_small"]=>
string(95) "http://photos-g.ak.facebook.com/photos-ak-sf2p/
v258/189/57/540709454/t540709454_467574_7613.jpg"
["src_big"]=>
string(95) "http://photos-g.ak.facebook.com/photos-ak-sf2p/
v258/189/57/540709454/n540709454_467574_7613.jpg"
["src"]=>
string(95) "http://photos-g.ak.facebook.com/photos-ak-sf2p/
v258/189/57/540709454/s540709454_467574_7613.jpg"
["link"]=>
string(57) "http://www.facebook.com/photo.php?
pid=467574&id=540709454"
["caption"]=>
string(9) "caption23"
["created"]=>
string(10) "1212610147"
......
[9]=>
array(8) {
["pid"]=>
string(19) "2322329421568483950"
["aid"]=>
string(19) "2322329421568036696"
["src_small"]=>
string(95) "http://photos-g.ak.facebook.com/photos-ak-sf2p/
v258/189/57/540709454/t540709454_467566_6827.jpg"
["src_big"]=>
string(95) "http://photos-g.ak.facebook.com/photos-ak-sf2p/
v258/189/57/540709454/n540709454_467566_6827.jpg"
["src"]=>
string(95) "http://photos-g.ak.facebook.com/photos-ak-sf2p/
v258/189/57/540709454/s540709454_467566_6827.jpg"
["link"]=>
string(57) "http://www.facebook.com/photo.php?
pid=467566&id=540709454"
["caption"]=>
string(8) "caption4"
["created"]=>
string(10) "1212610025"
}
[10]=>
array(8) {
["pid"]=>
string(19) "2322329421568483951"
["aid"]=>
string(19) "2322329421568036696"
["src_small"]=>
string(95) "http://photos-h.ak.facebook.com/photos-ak-sf2p/
v258/189/57/540709454/t540709454_467567_7106.jpg"
["src_big"]=>
string(95) "http://photos-h.ak.facebook.com/photos-ak-sf2p/
v258/189/57/540709454/n540709454_467567_7106.jpg"
["src"]=>
string(95) "http://photos-h.ak.facebook.com/photos-ak-sf2p/
v258/189/57/540709454/s540709454_467567_7106.jpg"
["link"]=>
string(57) "http://www.facebook.com/photo.php?
pid=467567&id=540709454"
["caption"]=>
string(8) "caption5"
["created"]=>
string(10) "1212610025"

There is this aid, it is the album id, and I wanted to only show
pictures of a special aid, so I wrote this:

for($i=0;$i<count($albums);$i++){
echo "<br><b>".$albums[$i]['name']."</b><br>";
$album_aid = $albums[$i]['aid'];
for($j=0;$j<count($photos);$j++){
$photo_aid = $photos[$j]['aid'];
if($photo_aid == $album_aid){
echo $album_aid." ".$photo_aid;
echo "<a href=\"".$photos[$j]['link']."\"><img src=\"".$photos[$j]
['src_small']."\" alt=\"Photo\" border=\"0\" /></a>";
}
}
echo "<br>";
}

but i keeps showing me all of the pictures, even if the aids are
different. I looked in the manual, but I do not know what I am doing
wrong. Can someone please help me?
Jun 27 '08 #1
10 1767
Da***********@gmail.com wrote:
but i keeps showing me all of the pictures, even if the aids are
different. I looked in the manual, but I do not know what I am doing
wrong. Can someone please help me?
You're doing arrays the wrong (newbie) way, and implementing a horrible
O(n^2) search algorithm.
First, always use foreach() to iterate through an array.

Second, if you're doing searches, don't iterate until you find the photo -
build the photos array in such way that the array key is the photo ID. e.g:

$photos[1234567] = array('link'=>'foobar' , ... );
Try it out.

--
----------------------------------
Iván Sánchez Ortega -ivansanchez-algarroba-escomposlinux-punto-org-

Un ordenador no es un televisor ni un microondas, es una herramienta
compleja.
Jun 27 '08 #2
In article <g3**********@hercules.cohp1>,
Iván Sánchez Ortega <ivansanchez-alg@rroba-escomposlinux.-.punto.-.orgwrote:
>O(n^2) search algorithm.

First, always use foreach() to iterate through an array.
foreach() is also O(n^2), for a 2-D array.

-A
Jun 27 '08 #3
Da***********@gmail.com wrote:
>
There is this aid, it is the album id, and I wanted to only show
pictures of a special aid, so I wrote this:

for($i=0;$i<count($albums);$i++){
echo "<br><b>".$albums[$i]['name']."</b><br>";
$album_aid = $albums[$i]['aid'];
for($j=0;$j<count($photos);$j++){
$photo_aid = $photos[$j]['aid'];
if($photo_aid == $album_aid){
echo $album_aid." ".$photo_aid;
echo "<a href=\"".$photos[$j]['link']."\"><img src=\"".$photos[$j]
['src_small']."\" alt=\"Photo\" border=\"0\" /></a>";
}
}
echo "<br>";
}

but i keeps showing me all of the pictures, even if the aids are
different. I looked in the manual, but I do not know what I am doing
wrong. Can someone please help me?
Where do you think you are "selecting" a special aid? This code goes
through all of the albums, and for each album, it prints all of the
pictures from that album. Translation: print all of the pictures.
--
Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
Jun 27 '08 #4
On 24 Jun, 05:33, a...@spamcop.net (axlq) wrote:
In article <g3pnje$83...@hercules.cohp1>,
Iván Sánchez Ortega *<ivansanchez-...@rroba-escomposlinux.-.punto.-..orgwrote:
O(n^2) search algorithm.
First, always use foreach() to iterate through an array.

foreach() is also O(n^2), for a 2-D array.

-A
Yes, but Iván was only offering that part as a general piece of advice.
Jun 27 '08 #5
On 24 Jun., 06:37, Tim Roberts <t...@probo.comwrote:
DavidSeck....@gmail.com wrote:
There is this aid, it is the album id, and I wanted to only show
pictures of a special aid, so I wrote this:
for($i=0;$i<count($albums);$i++){
* *echo "<br><b>".$albums[$i]['name']."</b><br>";
* *$album_aid = $albums[$i]['aid'];
* *for($j=0;$j<count($photos);$j++){
* * * * * *$photo_aid = $photos[$j]['aid'];
* * * * * *if($photo_aid == $album_aid){
* * * * * * * * * *echo $album_aid." ".$photo_aid;
* * * * * * * * * *echo "<a href=\"".$photos[$j]['link']."\"><img src=\"".$photos[$j]
['src_small']."\" alt=\"Photo\" border=\"0\" /></a>";
* * * * * *}
* *}
* *echo "<br>";
}
but i keeps showing me all of the pictures, even if the aids are
different. I looked in the manual, but I do not know what I am doing
wrong. Can someone please help me?

Where do you think you are "selecting" a special aid? *This code goes
through all of the albums, and for each album, it prints all of the
pictures from that album. *Translation: print all of the pictures.
--
Tim Roberts, t...@probo.com
Providenza & Boekelheide, Inc.
and this:
if($photo_aid == $album_aid){
? There I check if the photo belongs to the current album, if it does
I print it. But it keeps printing all of the pictures even if aid is
different
Jun 27 '08 #6
please I need help!
I tried this foreach but it doesn't change anything :(
Jun 27 '08 #7
still need help :(

pleaseeee
Jun 27 '08 #8
Da***********@gmail.com wrote:
>On 24 Jun., 06:37, Tim Roberts <t...@probo.comwrote:
>DavidSeck....@gmail.com wrote:
>but i keeps showing me all of the pictures, even if the aids are
different. I looked in the manual, but I do not know what I am doing
wrong. Can someone please help me?

Where do you think you are "selecting" a special aid? *This code goes
through all of the albums, and for each album, it prints all of the
pictures from that album. *Translation: print all of the pictures.

and this:
if($photo_aid == $album_aid){
? There I check if the photo belongs to the current album, if it does
I print it. But it keeps printing all of the pictures even if aid is
different
The inner loop prints all of the pictures in the album "album_aid" --
that's true. However, because of the outer loop, that loop will be run
once for each album. So, by the time that chunk is done, every picture in
every album will be printed.

Are you saying that you see every picture multiple times?
--
Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
Jun 27 '08 #9
Tim Roberts wrote:
Da***********@gmail.com wrote:
>On 24 Jun., 06:37, Tim Roberts <t...@probo.comwrote:
>>DavidSeck....@gmail.com wrote:

but i keeps showing me all of the pictures, even if the aids are
different. I looked in the manual, but I do not know what I am doing
wrong. Can someone please help me?
Where do you think you are "selecting" a special aid? This code goes
through all of the albums, and for each album, it prints all of the
pictures from that album. Translation: print all of the pictures.
and this:
if($photo_aid == $album_aid){
? There I check if the photo belongs to the current album, if it does
I print it. But it keeps printing all of the pictures even if aid is
different

The inner loop prints all of the pictures in the album "album_aid" --
that's true. However, because of the outer loop, that loop will be run
once for each album. So, by the time that chunk is done, every picture in
every album will be printed.

Are you saying that you see every picture multiple times?

Tim,I may be missing something as well, but as I see it the outer loop
only prints the names of all the albums, not their contents. The inner
loop should be selecting which content to print. See the comments below
inserted into his original code.

for($i=0;$i<count($albums);$i++){ // loop over all albums
echo "<br><b>".$albums[$i]['name']."</b><br>"; // Name is printed
$album_aid = $albums[$i]['aid']; // album linking parameter
for($j=0;$j<count($photos);$j++){ // loop over all photos
$photo_aid = $photos[$j]['aid']; // photo linking parameter
if($photo_aid == $album_aid){ // link parameters match test
echo $album_aid." ".$photo_aid; // show that they match
echo "<a href=\"".$photos[$j]['link']."\" // photo printed
<img src=\"".$photos[$j]
['src_small']."\" alt=\"Photo\" border=\"0\" /></a>";
}
}
echo "<br>";
}

Ican't find why this is happening to you, but from this logic that you
present it seems that a photo can belong to one, and only one, album.
So, why bother having two arrays? Why not simply have:
$albums[$i]['photo_list'] = $photos
where $photos is the array of photos belonging to that album? Do that
makes it that there is no need for looping over the inner array.

Also, as a suggestion for cleaner looking code why not try:

echo '<a href="' . $photos[$j]['link'] . '">' .
'<img src="' . $photos[$j]['src_small'] . '" ' .
'alt="Photo" border="0" /></a>';

That way you can avoid all those ugly quoting characters and it makes it
easier to find a missing or extra " in your code.
Jun 27 '08 #10
On Tue, 24 Jun 2008 13:36:22 -0700, <Da***********@gmail.comwrote:
please I need help!
I tried this foreach but it doesn't change anything :(
What did you change? You failed to post the code. I don't think you
understand, Iván pointed out an entirely different (better) approach, not
just changing to foreach.

--
Curtis, http://dyersweb.com
Jul 15 '08 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

26
by: William Park | last post by:
How do you compare 2 strings, and determine how much they are "close" to each other? Eg. aqwerty qwertyb are similar to each other, except for first/last char. But, how do I quantify that? ...
6
by: BrianJones | last post by:
I have a problem with the int strcmp(str1,str2) function: When I do: char *pass; char *passv; pass = getpass("Please enter....."); passv = getpass("Please verify.....");
41
by: Odd-R. | last post by:
I have to lists, A and B, that may, or may not be equal. If they are not identical, I want the output to be three new lists, X,Y and Z where X has all the elements that are in A, but not in B, and...
88
by: William Krick | last post by:
I'm currently evaluating two implementations of a case insensitive string comparison function to replace the non-ANSI stricmp(). Both of the implementations below seem to work fine but I'm...
1
by: Reg Rat | last post by:
Hello all, I'm led to believe that the .Net framework offers intelligent string comparison such that é follows e in an alphabetic sort rather than appearing after z, for example. Does this mean...
4
by: - Steve - | last post by:
The following line of vb.net code works fine: if(strAnswer.toUpper() = strUserAnswer.toUpper()) However in an ASP.net page I'm told Object reference not set to an instance of an object ...
2
by: Pugi! | last post by:
hi, I am using this code for checking wether a value (form input) is an integer and wether it is smaller than a given maximum and greater then a given minimum value: function...
10
by: william | last post by:
#include <stdio.h> int main() { char *str=NULL; char x="today is good!"; printf("%s", str); str=strtok(x," "); if (str=="today") //<==here is line that confuses me printf("they equals!\n");
13
by: Andreas Eibach | last post by:
Hi, let's say I have this: #include <string.h> #define BLAH "foo" Later on, I do this:
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.