Jump to content

need help with php

shadowss
Go to solution Solved by C2dan88,
35 minutes ago, shadowss said:

thought it just needed to be placed where >link<

Use concatenation (period operator) to join strings and variables together

https://www.php.net/manual/en/language.operators.string.php

 

echo "<a href='https://wits.ruc.dk/~jdns/wits01/opgave1.php'>" . $brugerobjekt['title'] . "</a><br>";

 

    $bruger_id = $_GET['id'];

 $posts = get_pids_by_uid($bruger_id);

  foreach ($posts as $pid) {
 
  echo "<br>";
  $brugerobjekt = get_post($pid);
  echo "<br>";
  echo $brugerobjekt['title'];
  echo "<br>";
  echo "<a href='https://wits.ruc.dk/~jdns/wits01/opgave1.php'>link</a>", "<br>";
  }

im a student thats trying to figure this out but teacher haven't really explained how to do things so im left on my own 
 

so i need to make the   echo $brugerobjekt['title']; into a link but im not sure how to cant really find any way to do so with only php and we are only allowed to use php i personally thought it just needed to be placed where >link< is after the actual link but no luck hope someone out there can actually help me with it 

Link to comment
Share on other sites

Link to post
Share on other sites

35 minutes ago, shadowss said:

thought it just needed to be placed where >link<

Use concatenation (period operator) to join strings and variables together

https://www.php.net/manual/en/language.operators.string.php

 

echo "<a href='https://wits.ruc.dk/~jdns/wits01/opgave1.php'>" . $brugerobjekt['title'] . "</a><br>";

 

Link to comment
Share on other sites

Link to post
Share on other sites

33 minutes ago, C2dan88 said:

Use concatenation (period operator) to join strings and variables together

https://www.php.net/manual/en/language.operators.string.php

 

echo "<a href='https://wits.ruc.dk/~jdns/wits01/opgave1.php'>" . $brugerobjekt['title'] . "</a><br>";

 

thx you so so so much i have been trying to figure this out for days but well teacher and such couldn't explain without just giving the answer so i was left to look through the internet trying to find a way 

which led me to remember linustechtips had a programming section so thank you so much for explaining and for the help 

Link to comment
Share on other sites

Link to post
Share on other sites

You also need to escape anything you output to a html page.

 

Basically, some characters like  <  > & ' " should not be output literally into the html page, but should be escaped with the equivalent codes &lt; &gt; &amp; and so on ..

At the very least, you use htmlspecialchars function : https://www.php.net/manual/en/function.htmlspecialchars.php

 

but a slightly better one would be htmlentities : https://www.php.net/manual/en/function.htmlentities.php

 

I used to use  it by making my own function like this :

 

<?php 

function html_esc($text) {
  return htmlentities($text, ENT_HTML5, 'UTF-8');
}

// if you expect the parameter to be a number, do some basic validation
// may want to use $_REQUEST instead which combines both $_GET and $_POST together

$bruger_id = intval($_GET['id']); 
if ($bruger_id !=0) {
 $posts = get_pids_by_uid($bruger_id);
 foreach ($posts as $pid) {
  echo "<br>";
  $brugerobjekt = get_post($pid);
  echo "<br>";
  echo html_esc($brugerobjekt['title']);
  echo "<br>";
  // use ' instead of " when you don't want PHP to parse the contents of a string
  // it's more correct to use " in html, if you want to use ', then you have to escape it as \'
  // if you use ", it will parse things like \t and replace variables inside string with values
  echo '<a href="https://wits.ruc.dk/~jdns/wits01/opgave1.php">' . html_esc($brugerobjekt['title']).' </a>, <br>';
 } 
}
?>

 

A good strategy is to have such functions that will be reused in lots of pages in a separate php file and simply include it in your other files with require_once.

 

For example, make a folder  "include" or "libraries" or whatever, and have the functions.php there then at the start of your php scripts you can say :

 

<?php 

require_once('include/functions.php');

?>

 

where include is a folder inside the folder where the php script is.  If your script is in a separate folder, you can do "../include/functions.php".. means up one folder.

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×