Array problem in PHP class. Please help

Discussion in 'PHP' started by smart, Jan 24, 2010.

  1. #1
    Hi Guys,

    I couldn't declare an array in a class and return it !!!

    Code is like this

    var $url = array ();
    var $ik = 0;

    foreach {
    $Link = someUrl ;
    $this ->url[$this->ik] = $Link;
    $this->ik = $this->ik + 1;

    echo"$this->ik and $this ->url[$this->ik]"; // this doesn't echo second part
    }

    how to store $Link in array and access it outside this class?

    Please help me. I got struck here.

    Thank you in advance.
    Smart
     
    smart, Jan 24, 2010 IP
  2. astkboy2008

    astkboy2008 Peon

    Messages:
    211
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #2
    what is the class?
     
    astkboy2008, Jan 24, 2010 IP
  3. K.Meier

    K.Meier Well-Known Member

    Messages:
    281
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    110
    #3
    If you could post the class it self we might be able to help.
     
    K.Meier, Jan 24, 2010 IP
  4. smart

    smart Active Member

    Messages:
    232
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    53
    #4
    This is a long but i highlighted this in RED.
    <?PHP
    # Original PHP code by Chirp Internet: www.chirp.com.au
    # Please acknowledge use of this code by including this header.

    class myAtomParser
    {
    var $urls= array();

    # keeps track of current and preceding elements
    var $tags = array();

    # array containing all feed data
    var $output = array();

    # return value for display functions
    var $retval = "";

    var $encoding = array();
    var $ik=0;

    # constructor for new object
    function myAtomParser($file)
    {
    # instantiate xml-parser and assign event handlers
    $xml_parser = xml_parser_create("");
    xml_set_object($xml_parser, $this);
    xml_set_element_handler($xml_parser, "startElement", "endElement");
    xml_set_character_data_handler($xml_parser, "parseData");

    # open file for reading and send data to xml-parser
    $fp = @fopen($file, "r") or die("<b>myAtomParser Error:</b> Could not open URL $file for input");
    while($data = fread($fp, 4096)) {
    xml_parse($xml_parser, $data, feof($fp)) or die(
    sprintf("myAtomParser: Error <b>%s</b> at line <b>%d</b><br>",
    xml_error_string(xml_get_error_code($xml_parser)),
    xml_get_current_line_number($xml_parser))
    );
    }
    fclose($fp);

    # dismiss xml parser
    xml_parser_free($xml_parser);
    }

    function startElement($parser, $tagname, $attrs)
    {
    if($this->encoding) {
    # content is encoded - so keep elements intact
    $tmpdata = "<$tagname";
    if($attrs) foreach($attrs as $key => $val) $tmpdata .= " $key=\"$val\"";
    $tmpdata .= ">";
    $this->parseData($parser, $tmpdata);
    } else {
    if($attrs['HREF'] && $attrs['REL'] && $attrs['REL'] == 'alternate') {
    $this->startElement($parser, 'LINK', array());
    $this->parseData($parser, $attrs['HREF']);
    $this->endElement($parser, 'LINK');
    }
    if($attrs['TYPE']) $this->encoding[$tagname] = $attrs['TYPE'];

    # check if this element can contain others - list may be edited
    if(preg_match("/^(FEED|ENTRY)$/", $tagname)) {
    if($this->tags) {
    $depth = count($this->tags);
    list($parent, $num) = each($tmp = end($this->tags));
    if($parent) $this->tags[$depth-1][$parent][$tagname]++;
    }
    array_push($this->tags, array($tagname => array()));
    } else {
    # add tag to tags array
    array_push($this->tags, $tagname);
    }
    }
    }

    function endElement($parser, $tagname)
    {
    # remove tag from tags array
    if($this->encoding) {
    if(isset($this->encoding[$tagname])) {
    unset($this->encoding[$tagname]);
    array_pop($this->tags);
    } else {
    if(!preg_match("/(BR|IMG)/", $tagname)) $this->parseData($parser, "</$tagname>");
    }
    } else {
    array_pop($this->tags);
    }
    }

    function parseData($parser, $data)
    {
    # return if data contains no text
    if(!trim($data)) return;
    $evalcode = "\$this->output";
    foreach($this->tags as $tag) {
    if(is_array($tag)) {
    list($tagname, $indexes) = each($tag);
    $evalcode .= "[\"$tagname\"]";
    if(${$tagname}) $evalcode .= "[" . (${$tagname} - 1) . "]";
    if($indexes) extract($indexes);
    } else {
    if(preg_match("/^([A-Z]+):([A-Z]+)$/", $tag, $matches)) {
    $evalcode .= "[\"$matches[1]\"][\"$matches[2]\"]";
    } else {
    $evalcode .= "[\"$tag\"]";
    }
    }
    }

    if(isset($this->encoding['CONTENT']) && $this->encoding['CONTENT'] == "text/plain") {
    $data = "<pre>$data</pre>";
    }

    eval("$evalcode .= '" . addslashes($data) . "';");
    }

    # display a single feed as HTML
    function display_feed($data, $limit)
    {
    extract($data);
    if($TITLE) {
    # display feed information
    $this->retval .= "<h1>";
    if($LINK) $this->retval .= "<a href=\"$LINK\" target=\"_blank\">";
    $this->retval .= stripslashes($TITLE);
    if($LINK) $this->retval .= "</a>";
    $this->retval .= "</h1>\n";
    if($TAGLINE) $this->retval .= "<P>" . stripslashes($TAGLINE) . "</P>\n\n";
    $this->retval .= "<div class=\"divider\"><!-- --></div>\n\n";
    }
    if($ENTRY) {
    # display feed entry(s)
    foreach($ENTRY as $item) {
    $this->display_entry($item, "FEED");
    if(is_int($limit) && --$limit <= 0) break;
    }
    }
    }

    # display a single entry as HTML
    function display_entry($data, $parent)
    {
    extract($data);
    if(!$TITLE) return;

    $this->retval .= "<p><b>";
    if($LINK) $this->retval .= "<a href=\"$LINK\" target=\"_blank\">";
    // getting into the Array
    $vURL = $LINK;
    $Strp=strpos($vURL, "&");
    $vURL= substr($vURL, 0, $Strp);
    //echo"URL is $vURL<br>";
    $ak = $this->ik;
    echo "$ak<br>";
    $this->urls = $vURL;
    //echo"URL is $this->urls<br>";
    $this->ik = $this->ik + 1;
    echo "$this->ik and URL is $this->urls[$ak]<br>";

    //$_POST[lecture_url][] = $vURL;
    $this->retval .= stripslashes($TITLE);
    $_POST[lecture_name][] = stripslashes($TITLE);
    if($LINK) $this->retval .= "</a>";
    $this->retval .= "</b>";
    if($ISSUED) $this->retval .= " <small>($ISSUED)</small>";
    $this->retval .= "</p>\n";

    if($AUTHOR) {
    $this->retval .= "<P><b>Author:</b> " . stripslashes($AUTHOR['NAME']) . "</P>\n\n";
    }
    if($CONTENT) {
    $this->retval .= "<P>" . stripslashes($CONTENT) . "</P>\n\n";
    } elseif($SUMMARY) {
    $this->retval .= "<P>" . stripslashes($SUMMARY) . "</P>\n\n";
    }
    }

    function fixEncoding($input, $output_encoding)
    {
    if(!function_exists('mb_detect_encoding')) return $input;

    $encoding = mb_detect_encoding($input);
    switch($encoding) {
    case 'ASCII':
    case $output_encoding:
    return $input;
    case '':
    return mb_convert_encoding($input, $output_encoding);
    default:
    return mb_convert_encoding($input, $output_encoding, $encoding);
    }
    }

    # display entire feed as HTML
    function getOutput($limit=false, $output_encoding='UTF-8')
    {
    $this->retval = "";
    $start_tag = key($this->output);

    switch($start_tag) {
    case "FEED":
    foreach($this->output as $feed) $this->display_feed($feed, $limit);
    break;
    default:
    die("Error: unrecognized start tag '$start_tag' in getOutput()");
    }

    return $this->fixEncoding($this->retval, $output_encoding);
    }

    # return raw data as array
    function getRawOutput($output_encoding='UTF-8')
    {
    return $this->fixEncoding($this->output, $output_encoding);
    }
    }
    ?>
     
    smart, Jan 24, 2010 IP
  5. sickanimations

    sickanimations Peon

    Messages:
    137
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Can I suggest using www.pastbin.ca or something next time? Makes it a bit tidier :)
     
    sickanimations, Jan 24, 2010 IP
  6. sickanimations

    sickanimations Peon

    Messages:
    137
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #6
    I wrote a basic class scope example which is analogous to what you want to achieve here http://pastebin.ca/1763973 (less than 30 lines)
     
    sickanimations, Jan 24, 2010 IP
  7. smart

    smart Active Member

    Messages:
    232
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    53
    #7
    I cant fix it guys. Can any one ready to help i can send the files? It takes only few minutes for you but it is a big deal for me.

    Thank you guys.
     
    smart, Jan 24, 2010 IP
  8. astkboy2008

    astkboy2008 Peon

    Messages:
    211
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0