Jump to content

Need help w using modified url for the first time

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
7 replies to this topic

#1
Darkone85

Darkone85

    Learning Programmer

  • Members
  • PipPipPip
  • 30 posts
Our assignment is to by using three links be able to sort, randomize and print a vektor. In the modurl.php which I created Vektor.php is included (and pre-made) and have methods in it for this.

The modurl code looks like this:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"xml:lang="sv" >
<head>
<title>
</title>
</head>
<body>
    
<?php
include "Vektor.php";
$var_vek = new Vektor();
$hidden;

if(isset($_GET["action"])){
$action = $_GET["action"];


switch ($action){

case "Slumpa":
$var_vek->randomVektor();
$hidden=$var_vek->vektor2String();
echo("Vektorn är slumpad");
break;

case "Sortera":
$var_vek->string2Vektor($_GET["hidden"]);
$var_vek->sortVektor();
$hidden=$var_vek->vektor2String();
echo("Vektorn är sorterad");
break;

case "Skriv ut":
$var_vek->string2Vektor($_GET["hidden"]);
echo($var_vek->formattedVektor());
$hidden=$var_vek->vektor2String();
break;

default:
    echo("Något är fel");
    break;
}
}
?>

<a href="modurl.php?action=Slumpa&$var_vek=$hidden">Slumpa</a><br />
<a href="modurl.php?action=Sortera&$var_vek=$hidden">Sortera</a><br />
<a href="modurl.php?action=Skriv ut&$var_vek=$hidden">Skriv ut</a><br />

</body>
</html>


and the Vektor.php like this:

<?php  

class Vektor
  {
   //Privat attribut för klassen Vektor.
   //$int_vektor kommer att innehåller vektorn.
   private $int_vektor;

   //Konstruktor för klassen Vektor.
   //Skapar 100 platser (0-99) för $int_vektor och tilldelar varje plats värdet noll (0).
   function __construct()
   {
         //array array_fill  ( int $start_index  , int $num  , mixed $value  )
          $this->int_vektor = array_fill(0, 100, 0);
   }

   //Destruktor för klassen Vektor.
   //Frigör tar bort $int_vektor.
   function __destruct()
   {
       unset($this->int_vektor);
   }

   //Metoden string2Vektor() skapar och returnerar en vektor av strängar.
   //Inkommande parameter skall vara en sträng bestående av siffror och kommatecken (,).
   //Vektorn av strängar tilldelas $int_vektor.
   public function string2Vektor($inString)
   {
    $this->int_vektor = explode(",", $inString);
   }

   //Metoden vektor2String() skapar en sträng av en vektor.
   //Strängen som returneras består av siffor och kommatecken (,).
   public function vektor2String()
   {
    return implode(",", $this->int_vektor);
   }

   //Metoden setVektor() tilldelar $int_vektor en ny vektor.
   //Inkommande parameter är den nya vektorn.
   public function setVektor($inVektor)
  {
      $this->int_vektor = $inVektor;
  }

  //Metoden getVektor returnerar aktuell vektor.
  public function getVektor()
  {
      return $this->int_vektor;
  }

  //Metoden randomVektor() slumpar 100 tal mellan 1 och 1000 och placerar dessa i $int_vektor.
  public function randomVektor()
  {
   for($int_raknare = 0; $int_raknare < 100; $int_raknare++)
   {
        $this->int_vektor[$int_raknare] = mt_rand(1, 1000);
   }
  }

  //Metoden sortVektor() sorterar talen i $int_vektor i stigande ordning.
  public function sortVektor()
  {
   sort($this->int_vektor);
  }

   //Metoden formattedVektor() skapar och returnerar en xhtml-tabell innehållande värdena i $_int_vektor.
   public function formattedVektor()
   {
       $str_output = "<table border=\"1\">";
        $int_arraycounter = 0;

        for($int_outercounter = 1; $int_outercounter <= 10; $int_outercounter++)
       {
         $str_output .= "<tr align=\"center\">\n";
          for($int_innercounter = 1; $int_innercounter <= 10; $int_innercounter++)
          {

                $str_output .= "<td>".$this->int_vektor[$int_arraycounter]."</td>\n";
                $int_arraycounter++;
          }

         $str_output .= "</tr>\n";
        }

        $str_output .= "</table>\n";

        return $str_output;
   }
  }

?>


The error message is Notice: Undefined index: hidden in C:\wamp\www\modurl.php on line 28 and 35 which are the lines:
$var_vek->string2Vektor($_GET["hidden"]); and
$var_vek->string2Vektor($_GET["hidden"]);

with hidden as the error. I havent fully understood this and dont know what to GET, if I knew i woukldnt post :) So, what is it that im looking for to replace it with, is it maybe action??

Thanks in advance,
Manne

Edited by Jaan, 20 April 2010 - 03:08 AM.
Please use code tags when you are posting your codes!


#2
Darkone85

Darkone85

    Learning Programmer

  • Members
  • PipPipPip
  • 30 posts
Update: have fixed some things so atm the randomizing is working fine, but not the sorting.

My phpfile:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"xml:lang="sv" >
<head>
<title>
</title>
</head>
<body>
    
<?php
include "Vektor.php";
$var_vek = new Vektor();
$hidden;

if(isset($_GET["action"])){
$action = $_GET["action"];

$vektor="";
if(isset($_GET["vektor"])){
	$vektor = $_GET["vektor"];
}

switch ($action){

case "Slumpa":
$var_vek->randomVektor();
$hidden=$var_vek->vektor2String();
echo("Vektorn är slumpad");
break;

case "Sortera":
$var_vek->string2Vektor($_GET["action"]);
$var_vek->sortVektor();
$hidden=$var_vek->vektor2String();
echo("Vektorn är sorterad");
break;

case "Skriv ut":
$var_vek->string2Vektor($vektor);
echo($var_vek->formattedVektor());
$hidden=$var_vek->vektor2String();
break;

default:
    echo("Något är fel");
	break;
}
}
?>

<a href="modurl.php?action=Slumpa&$var_vek=$hidden">Slumpa</a><br />
<a href="modurl.php?action=Sortera&$var_vek=$hidden">Sortera</a><br />
<a href="modurl.php?action=Skriv ut&vektor=<?php echo $hidden?>">Skriv ut</a><br />

</body>
</html>

and the included Vektor:
<?php  

class Vektor
  {
   //Privat attribut för klassen Vektor.
   //$int_vektor kommer att innehåller vektorn.
   private $int_vektor;

   //Konstruktor för klassen Vektor.
   //Skapar 100 platser (0-99) för $int_vektor och tilldelar varje plats värdet noll (0).
   function __construct()
   {
   	  //array array_fill  ( int $start_index  , int $num  , mixed $value  )
          $this->int_vektor = array_fill(0, 100, 0);
   }

   //Destruktor för klassen Vektor.
   //Frigör tar bort $int_vektor.
   function __destruct()
   {
   	unset($this->int_vektor);
   }

   //Metoden string2Vektor() skapar och returnerar en vektor av strängar.
   //Inkommande parameter skall vara en sträng bestående av siffror och kommatecken (,).
   //Vektorn av strängar tilldelas $int_vektor.
   public function string2Vektor($inString)
   {
    $this->int_vektor = explode(",", $inString);
   }

   //Metoden vektor2String() skapar en sträng av en vektor.
   //Strängen som returneras består av siffor och kommatecken (,).
   public function vektor2String()
   {
    return implode(",", $this->int_vektor);
   }

   //Metoden setVektor() tilldelar $int_vektor en ny vektor.
   //Inkommande parameter är den nya vektorn.
   public function setVektor($inVektor)
  {
  	$this->int_vektor = $inVektor;
  }

  //Metoden getVektor returnerar aktuell vektor.
  public function getVektor()
  {
  	return $this->int_vektor;
  }

  //Metoden randomVektor() slumpar 100 tal mellan 1 och 1000 och placerar dessa i $int_vektor.
  public function randomVektor()
  {
   for($int_raknare = 0; $int_raknare < 100; $int_raknare++)
   {
        $this->int_vektor[$int_raknare] = mt_rand(1, 1000);
   }
  }

  //Metoden sortVektor() sorterar talen i $int_vektor i stigande ordning.
  public function sortVektor()
  {
   sort($this->int_vektor);
  }

   //Metoden formattedVektor() skapar och returnerar en xhtml-tabell innehållande värdena i $_int_vektor.
   public function formattedVektor()
   {
   	$str_output = "<table border=\"1\">";
        $int_arraycounter = 0;

    	for($int_outercounter = 1; $int_outercounter <= 10; $int_outercounter++)
   	{
   	  $str_output .= "<tr align=\"center\">\n";
          for($int_innercounter = 1; $int_innercounter <= 10; $int_innercounter++)
          {

                $str_output .= "<td>".$this->int_vektor[$int_arraycounter]."</td>\n";
                $int_arraycounter++;
          }

         $str_output .= "</tr>\n";
        }

        $str_output .= "</table>\n";

        return $str_output;
   }
  }

?>

The line thats not working properly is: $var_vek->string2Vektor($_GET["action"]); and we dont know how to proceed, we tried to change "action" to vektor, hidden and so on...

Anyone?

#3
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,299 posts
$var_vek->string2Vektor($_GET["action"]);

needs to be

$var_vek->string2Vektor($_GET["hidden"]);
as it's your hidden parameter that is the vector content to be...
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall

#4
Vswe

Vswe

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 9,552 posts
Yeah but the GET value is vektor:

<a href="modurl.php?action=Skriv ut&[B]vektor=[/B]<?php echo $hidden?>">Skriv ut</a><br />


#5
Darkone85

Darkone85

    Learning Programmer

  • Members
  • PipPipPip
  • 30 posts
UPDATED CODE:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"xml:lang="sv" >
<head>
<title>
</title>
</head>
<body>
    
<?php
include "Vektor.php";
$var_vek = new Vektor();
$hidden="";

if(isset($_GET["action"])){
$action = $_GET["action"];

$vektor="";
if(isset($_GET["vektor"])){
	$vektor = $_GET["vektor"];
}

switch ($action){

case "Slumpa":
$var_vek->randomVektor();
$hidden=$var_vek->vektor2String();
echo("Vektorn är slumpad");
break;

case "Sortera":
$var_vek->string2Vektor($vektor);
$var_vek->sortVektor();
$hidden=$var_vek->vektor2String();
echo("Vektorn är sorterad");
break;

case "Skriv ut":
$var_vek->string2Vektor($vektor);
echo($var_vek->formattedVektor());
//$hidden=$var_vek->vektor2String();
break;

default:
    echo("Något är fel");
	break;
}
}
?>

<a href="modurl.php?action=Slumpa&vektor=$hidden">Slumpa</a><br />
<a href="modurl.php?action=Sortera&vektor=$hidden">Sortera</a><br />
<a href="modurl.php?action=Skriv ut&vektor=<?php echo $hidden?>">Skriv ut</a><br />

</body>
</html>



#6
Darkone85

Darkone85

    Learning Programmer

  • Members
  • PipPipPip
  • 30 posts
Still same error: Notice: Undefined offset: 1 in C:\wamp\www\Vektor.php on line 79

Notice: Undefined offset: 2 in C:\wamp\www\Vektor.php on line 79

Notice: Undefined offset: 3 in C:\wamp\www\Vektor.php on line 79

and so on when i try to print (Skriv ut) after case Sortera. To randomize and then Skriv ut is no problem

#7
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,299 posts
the laast rows should be like this:
default:
    echo("Något är fel");
    break;
}
}


echo '<a href="modurl.php?action=Slumpa">Slumpa</a><br />';
echo '<a href="modurl.php?action=Sortera&vektor='.$hidden.'">Sortera</a><br />';
echo '<a href="modurl.php?action=Skriv ut&vektor='.$hidden.'">Skriv ut</a><br />';
?>
</body>
</html>
then it will give good links
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall

#8
Darkone85

Darkone85

    Learning Programmer

  • Members
  • PipPipPip
  • 30 posts
Hail the moderators!! <3