Jump to content

Array Returning Last Value in Database


dtrapp

Recommended Posts

I really wanted to solve this on my own, I spent over 4 hours yesterday and thought I had it so many times - but it's just not working the way it should.

 

To preface, I'm trying to put together a sitemap that creates the URL's from keywords in a database and appending them to a preformed URL. (i.e. mysite.com/id.php?ID=$KW).

 

For a reference, the original script is here - http://bit.ly/3TuASS

 

I have only tried to modify this script to work the way I need it, and you'll see I haven't changed it much. Ok, on to the code.

 

<?php
header("Content-Type: text/xml; charset=utf-8");

//took out DB info, but I promise it's there 

$sql = sprintf('SELECT DISTINCT kw FROM keywords');
$result = mysql_query($sql) or die("No data found.");

$xml_output = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";

$xml_output .= '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';

$xml_output .= "<url>
<loc>http://www.thesite.com/</loc>
<lastmod>2009-04-11</lastmod>
<changefreq>monthly</changefreq>
<priority>1</priority>
</url>";

while ($row = mysql_fetch_assoc($result)) { $loc = 'http://www.thesite.com/id.php?ID=' . $row['kw']; 
}



    $xml_output  .= "\t<url>\n";
    $xml_output  .= "\t\t<loc>" . $loc . "</loc>\n";
    $xml_output  .= "\t\t<lastmod>2009-04-11</lastmod>\n";
    $xml_output  .= "\t\t<changefreq>daily</changefreq>\n";
    $xml_output  .= "\t\t<priority>1.0</priority>\n";

     
    
    $xml_output .= "\t</url>\n";


$xml_output .= "</urlset>";

echo $xml_output;

?> 

 

A friend that I was asking for help over IM suggested using the "while" instead of the "for" example in the original script. The end result is, it doesn't error out - but it doesn't work correctly either. In fact, it's more frustrating because I either get this result.

 

hundreds of this exact URL in a properly formed XML doc: http://www.thesite.com/id.php?ID=array

 

or this...

 

a properly formed XML document with exactly 1 result containing the last keyword in the db: http://www.thesite.com/id.php?ID=LastKeywordInDB.

 

My friend gave me a suggestion that I didn't really know how to implement, he said "you should echo out $loc in the loop". I get the theory, but the implementation I obviously have wrong. The good news is that if you echo $loc within the brackets in the while statement, the page will give an "XML not properly formed" error BUT will also show a horizontal line of all 300+ URL's with keywords appended, and although it's not a correct XML document, it does show the values in the array are there.

 

So from this I've gathered the array is working, I'm just not associating or displaying it correctly. I've tried to be as descriptive as possible explaining this issue and chances are it's probably just a small mistake, but hours of searching online for a possible answer that I could both understand and implement was futile. I'm hoping someone here can help.

 

Thanks.

 

 

Link to comment
https://forums.phpfreaks.com/topic/153744-array-returning-last-value-in-database/
Share on other sites

add error_reporting(E_ALL); to the top of the file.

 

change this...

$xml_output = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";

to this...

$xml_output = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";

 

see if anything turns up.

adding

 

$xml_output = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";

 

provided the same result of...

 

This XML file does not appear to have any style information associated with it. The document tree is shown below.
      
−
<urlset>
−
<url>
<loc>http://www.thesite.com/</loc>
<lastmod>2009-04-11</lastmod>
<changefreq>monthly</changefreq>
<priority>1</priority>
</url>
−
<url>
−
<loc>
http://www.thesite.com/id.php?ID=lastkeywordinDB
</loc>
<lastmod>2009-04-11</lastmod>
<changefreq>daily</changefreq>
<priority>1.0</priority>
</url>
</urlset>

 

Thanks for the quick reply though.

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.