1

enter image description hereBeen stuck on this problem for the last few hours, in order to implement portfolio I started by practicing, the idea was to call skroob's column of symbol, lets say I manually added two symbols there and than using a loop, call lookup for each symbol and print out the price, name and symbol through the portfolio.php file. I kind of know a bit why I am getting this error but would appreciate if someone much further explain it to me why am I getting this and most importantly how to do what I have explained I want to do

code for index.php

<?php

// configuration
require("../includes/config.php"); 

$positions = []; 
$rows = CS50::query ("SELECT symbol FROM portfolios WHERE user_id = ?", $_SESSION["id"]);

// lookup for quotes for every symbol
foreach ($rows as $row)
{
    $stock = lookup($row);

    $positions = ["name" => $stock["name"],
                  "price" => number_format($stock["price"], 2, '.', ''),
                  "symbol" => $stock["symbol"]];

}



// render portfolio
render("portfolio.php", ["title" => "Portfolio", "positions" => $positions]);

?>

code for portfolio.php

<div>

    foreach ($positions as $position)
    {
        print("<tr>");
        print("<td>{$positions["symbol"]}</td>");
        print("<td>{$positions["name"]}</td>");
        print("<td>{$positions["price"]}</td>");
        print("</tr>");
    }

?>

0

Look at helpers.php, specifically on lines 58 and 64, where the error message indicates that you have a problem. Try to think it through for yourself.

In general, here's how to reason through something like this:

  • what function in helpers.php is being called?

Line 58 and line 64, where the errors are occurring, are part of the lookup() function:

function lookup($symbol)
{
    // reject symbols that start with ^
    if (preg_match("/^\^/", $symbol))
    {
        return false;
    }

    // reject symbols that contain commas
    if (preg_match("/,/", $symbol))
    {
        return false;
    }

The error message says that the preg_match() function expects a string to be passed as its second argument ($symbol), but it's receiving an array instead.

  • where is that function call occurring in portfolio.php?
// lookup for quotes for every symbol
foreach ($rows as $row)
{
    $stock = lookup($row);
  • what variables are being passed as arguments to the function?

You are passing $row, which is an array.

  • what are the values of those variables? (you might want to dump() them and see!)

You should do this yourself. The dump() function is provided in helpers.php, and its usage is explained in the problem specification.

Add a debugging line that says:

dump($row);

Put it inside the foreach ($rows as $row) loop, but before the $stock = lookup($row); line. The debugging output from this should help you to determine how to pass the correct argument (which is not the entire row, but just the string value associated with the ["symbol"] index!)

  • most importantly, what are the data types of those variables? Are you passing an array value when the function expects a string?

Yes, you are passing an array when you mean to pass a string, just as the error message says.

Honestly, past a certain point I'm not doing you any favors by spelling everything out for you. You need to practice reasoning through the debugging process yourself. I believe that you are capable of doing so!

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .