By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,716 Members | 1,317 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,716 IT Pros & Developers. It's quick & easy.

Processing a db query - tough problem and need help

P: n/a
Let's I do a mysql query and then I do a,
for( $i = 1; $row = mysql_fetch_array($result); $i++ ) {...}
and it gives me this:

PageID Title URL Description
1 lee's gogle1.com This is lee's website.
1 lee's gogle2.com This is lee's website.
2 Jon's yaho1.com This is Jon's website.
2 Jon's yaho2.com This is Jon's website.

But I want this:

PageID Title URL Description
1 lee's gogle1.com gogle2.com This is lee's website.
2 Jon's yaho1.com yaho2.com This is Jon's website.

How??? I'm at a loss to figure this out! Thanks,
Lee G.
Wash DC

Jul 17 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
I think that I have had to do something like this before. I believe what I
ended up doing was creating an array for each, in your case, title. The
array would not hold the title names but rahter the URLs. What you would do,
is create a for loop before the for loop that you listed prior. In this loop
you would run through the rows and have a variable called $lastUrl that
would hold the value of the URL in the previous row. Then you check the
current row and if it equals the the $lastUrl then just concatenate it to
the previous array value. If it doesn't equal, than insert a new item in the
array.

$count = 0;
for( $i = 1; $row = mysql_fetch_array($result); $i++ )
{
$lastUrl = '';
if($lastUrl == $row['url'])
{
$row[$count] .= $row['url'];
}
else
{
$row[$count += 1] = $row['url']
}
}

With this method you may have to do the same with Title because you will
want the array's to match up. See if this works.
-Kyle Hayes

"leegold2" <le*****@nospam.net> wrote in message
news:wA0bd.5245$gd1.3993@trnddc08...
Let's I do a mysql query and then I do a,
for( $i = 1; $row = mysql_fetch_array($result); $i++ ) {...}
and it gives me this:

PageID Title URL Description
1 lee's gogle1.com This is lee's website.
1 lee's gogle2.com This is lee's website.
2 Jon's yaho1.com This is Jon's website.
2 Jon's yaho2.com This is Jon's website.

But I want this:

PageID Title URL Description
1 lee's gogle1.com gogle2.com This is lee's website.
2 Jon's yaho1.com yaho2.com This is Jon's website.

How??? I'm at a loss to figure this out! Thanks,
Lee G.
Wash DC

Jul 17 '05 #2

P: n/a
for as far as I see, there is nothing wrong with this line of code...
could you post the entire loop, because I think it goes wrong in there

leegold2 wrote:
Let's I do a mysql query and then I do a,
for( $i = 1; $row = mysql_fetch_array($result); $i++ ) {...}
and it gives me this:

PageID Title URL Description
1 lee's gogle1.com This is lee's website.
1 lee's gogle2.com This is lee's website.
2 Jon's yaho1.com This is Jon's website.
2 Jon's yaho2.com This is Jon's website.

But I want this:

PageID Title URL Description
1 lee's gogle1.com gogle2.com This is lee's website.
2 Jon's yaho1.com yaho2.com This is Jon's website.

How??? I'm at a loss to figure this out! Thanks,
Lee G.
Wash DC

Jul 17 '05 #3

P: n/a
leegold2 wrote:
Let's I do a mysql query and then I do a,
for( $i = 1; $row = mysql_fetch_array($result); $i++ ) {...}
and it gives me this:

PageID Title URL Description
1 lee's gogle1.com This is lee's website.
1 lee's gogle2.com This is lee's website.
2 Jon's yaho1.com This is Jon's website.
2 Jon's yaho2.com This is Jon's website.

But I want this:

PageID Title URL Description
1 lee's gogle1.com gogle2.com This is lee's website.
2 Jon's yaho1.com yaho2.com This is Jon's website.


I guess you have a problem with your data structure. If there are more than
1 URLs per page I suggest to handle URLs in separate tables:

Table pages:
- PageID
- Title
- Description

Table urls:
- PageID (foreign key)
- URL

So you can assign as many URLs to a page as you want. Then you can do a new
query to get the URLs inside the loop, such as

for ($i = 1; $row = mysql_fetch_array($result); $i++ ) {
$result2 = mysql_query("SELECT URL FROM urls WHERE
PageID=".$row['PageID']);
while ($row2 = mysql_fetch_array($result2) {...}
}

which is not very elegant but easy.

HTH
Markus
Jul 17 '05 #4

P: n/a
Markus Ernst wrote:
leegold2 wrote:
Let's I do a mysql query and then I do a,
for( $i = 1; $row = mysql_fetch_array($result); $i++ ) {...}
and it gives me this:

PageID Title URL Description
1 lee's gogle1.com This is lee's website.
1 lee's gogle2.com This is lee's website.
2 Jon's yaho1.com This is Jon's website.
2 Jon's yaho2.com This is Jon's website.

But I want this:

PageID Title URL Description
1 lee's gogle1.com gogle2.com This is lee's website.
2 Jon's yaho1.com yaho2.com This is Jon's website.

I guess you have a problem with your data structure. If there are more than
1 URLs per page I suggest to handle URLs in separate tables:

Table pages:
- PageID
- Title
- Description

Table urls:
- PageID (foreign key)
- URL

So you can assign as many URLs to a page as you want. Then you can do a new
query to get the URLs inside the loop, such as


Yeah, I eneded up putting a query inside the loop - Thanks!!!


for ($i = 1; $row = mysql_fetch_array($result); $i++ ) {
$result2 = mysql_query("SELECT URL FROM urls WHERE
PageID=".$row['PageID']);
while ($row2 = mysql_fetch_array($result2) {...}
}

which is not very elegant but easy.

HTH
Markus

Jul 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.