/    Sign up×
Community /Pin to ProfileBookmark

Looping return mysql array

I’m sure this is a simple thing, however, I can’t seem to figure it out…

I have an array pulled from mysql

[CODE]
$DATA=array();

$query=mysql_query(“SELECT * FROM ads_national WHERE type=’$type’ ORDER BY rand() “);
$num_rows = mysql_num_rows($query);
if($num_rows > 0) {
while($ROW = mysql_fetch_array($query, MYSQL_ASSOC) ) {
$DATA[] = $ROW;
}
}
return $DATA;
[/CODE]

$DATA now contains 4 rows of info including fields url, alt and imgName

I normally create a loop to display the data how I want such as

[CODE]
foreach($DATA as $row) {
echo $row[‘url’];
echo $row[‘alt’];
echo $row[‘imgName’];
}

[/CODE]

The problem I have is I don’t want to display all 4 rows in the same place. I want to display the first 2 rows in one area of the page, and the last 2 rows somewhere else on the page.

It seems I should do something like this, but its not working

[CODE]
//display this row here….

echo $DATA[0][‘url’];
echo $DATA[0][‘alt];
echo $DATA[0][”imgName’];

… some other html/php

//display this row somewhere else

echo $DATA[1][‘url’];
echo $DATA[1][‘alt];
echo $DATA[1][”imgName’];
[/CODE]

How do I take that array and display its values in various places on the page without the loop?

Thanks in advance.

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@svidgenMay 03.2010 — Assuming the duplicate single-quote here is present only in this post--not your actual code ...
echo $DATA[0][''imgName'];[/QUOTE]

I don't see any issue with what you've posted. Can we see more of the code? I imagine the results are being affected by surrounding code -- possibly some scope issues.
Copy linkTweet thisAlerts:
@SrWebDeveloperMay 03.2010 — Try this:

[code=php]$DATA[$counter]=$ROW; $counter++;[/code]

But I agree using [] should have worked also.
×

Success!

Help @waMP spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.28,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...