Richard
Richard

Reputation: 209

Arrays within variables

I am trying to produce an array of values to be used by a variable, but I can't get it to work. I don't want to "see" the results. Instead, I want the variable to "use" the results. Here's my code:

$rss_results = mysql_query("SELECT feed_id, feed_url, feed_title, feed_order, feed_page_id FROM user_feeds  WHERE ((feed_owner = '" . $_SESSION['UserId'] . "'));");

    if ($rss_results) {

    while($row = mysql_fetch_array($rss_results))
    {
    $feed->set_feed_url(print "'".$row[feed_url]."',");

    }

    }
    else {
      // something went wrong.
      echo mysql_error();

The problem is with the line:

$feed->set_feed_url(print "'".$row[feed_url]."',");

EDIT 1) Yes. I know print doesn't belong there. I forgot to remove it after doing some troubleshooting earlier.

2) I am trying to use this code with an RSS parser calleld SimplePie.

3) Everyone seems to agree that the following code works

   $feed->set_feed_url(array("'" . $row['feed_url'] . "',"));

...but it doesn't. If I weren't accessing my db, the code would look something like this....

$feed->set_feed_url(array('http://www.tmz.com/rss.xml', 'feed://rss.cbc.ca/lineup/topstories.xml'));

...which works just fine. I am trying to reproduce that effect using values stored in a mysql db. If the code worked, it would be working. So there must be a problem with the code that is "supposed" to be working.

Upvotes: 0

Views: 120

Answers (1)

Matt
Matt

Reputation: 7040

When accessing an associative array, you must put the index name inside quotes:

Instead of

$row[feed_url]

use

$row['feed_url']

Also,

$feed->set_feed_url(print "'".$row[feed_url]."',");

makes no sense. If you want to pass a value to a method, pass the variable, don't print it to the screen.

$feed->set_feed_url("'" . $row['feed_url'] . "',");

If you don't want the string you pass to $feed->set_feed_url() to be enclosed in 's, just pass it like a regular variable:

$feed->set_feed_url($row['feed_url']);

UPDATE:

Since you mention you're trying to pass an array, you have to first initialize it, then populate it, then send it to the method:

$urls = array();
while ($row = mysql_fetch_array($rss_results)) {
    $urls[] = $row['feed_url'];
}
$feed->set_feed_url($urls);

Doing this:

$feed->set_feed_url(array("'" . $row['feed_url'] . "',"));

Will simply pass the following to $feed->set_feed_url():

array(
    [0] => String() "'[value of $row['feed_url'] ]',"
)

If you want to call $feed->set_feed_url() once for each iteration of the loop, this will work fine:

while($row = mysql_fetch_array($rss_results)) {
    $feed->set_feed_url(array($row['feed_url']));
}

Please note: You should stop using mysql_* functions. They're being deprecated. Instead use PDO (supported as of PHP 5.1) or mysqli (supported as of PHP 4.1). If you're not sure which one to use, read this SO article.

Upvotes: 1

Related Questions