Jump to content

need help with php arrays

shadowss

image.thumb.png.c890fd59041d227b7e5197054e43f936.png

hello im a student that dont understand how to make this link so what i need to do is make a list with all usernames and then i need to make those usernames into links but im only allowed to use php 

the get_uids is a function they made at my university that give you all the usernames in a array format hence why im using print_r() and i can print the array but it does not wanna make it into links (just makes a link after)so could anyone help me and explain to me what kind of function i would maybe have to use or help me figure it out or something cause im so confused of what to do 
and before anyone say ask teacher have done thats why i got more confused

Link to comment
Share on other sites

Link to post
Share on other sites

already figured it out how to do it the solution was 

 

<?php

require_once '/var/www/wits.ruc.dk/db.php';

$brugere = get_uids(); // får fat i all bruger

foreach ($brugere as $bruger_id) {
    $bruger = get_user($bruger_id); // definere vores specifikke bruger 

    $link = "https://wits.ruc.dk/~jdns/wits01/opgaveb.php" . $bruger_id; // definere vores link 

    echo "<a href='$link'>" . $bruger['uid'] . "</a><br>"; // echoer navn ud og laver dem som et link til opgaveb
}

?>

so i put it into a foreach and used a function the school made up and then defined our link then echoed it as a href with our user as a string 

Link to comment
Share on other sites

Link to post
Share on other sites

I don't think that's quite right

 

If you want to give the ID as a parameter to that opgaveb.php , you'd want to put the parameter name after the .php extension of the script.

 

Like for example the final link would be https://wits.ruc.dk/~jdns/wits01/opgaveb.php?id=<your id here>

 

Right now, you're just gluing the ID to the end of the url, ex opgaveb.php1 , opgaveb.php3  and so on.

 

As an improvement, you may want to build the sql query and retrieve all the users using one query, instead of running one sql query for each user.

You would build a query like  SELECT * FROM users WHERE user_id IN (id1, id2, id3, ... , idn) ORDER BY <user_id or user_name, whatever> ASC

 

You can get all results and put them into an array let's call it user_records, and then do the foreach with that user_records array .

Link to comment
Share on other sites

Link to post
Share on other sites

On 10/1/2023 at 3:34 PM, mariushm said:

I don't think that's quite right

 

If you want to give the ID as a parameter to that opgaveb.php , you'd want to put the parameter name after the .php extension of the script.

 

Like for example the final link would be https://wits.ruc.dk/~jdns/wits01/opgaveb.php?id=<your id here>

 

Right now, you're just gluing the ID to the end of the url, ex opgaveb.php1 , opgaveb.php3  and so on.

 

As an improvement, you may want to build the sql query and retrieve all the users using one query, instead of running one sql query for each user.

You would build a query like  SELECT * FROM users WHERE user_id IN (id1, id2, id3, ... , idn) ORDER BY <user_id or user_name, whatever> ASC

 

You can get all results and put them into an array let's call it user_records, and then do the foreach with that user_records array .

first of we are not using sql so using sql is not an option i was only as stated allowed to use php and as you stated i was gluing the ID at the end of the url which was what was needed i did forget to add the single thing i changed in the code but that was like 5 characters 
https://wits.ruc.dk/~jdns/wits01/opgaveb.php?id=       was like this instead of the one under 
https://wits.ruc.dk/~jdns/wits01/opgaveb.php

but at the end of it the code was right and worked 
there is always different ways to do coding no code can only be written in one way 

but i will admit being able to use sql for the server would have been much easier and a better solution than the solution they came up with but would then also mean we have to learn sql too while learning php and html

Link to comment
Share on other sites

Link to post
Share on other sites

5 hours ago, shadowss said:

first of we are not using sql so using sql is not an option i was only as stated allowed to use php and as you stated i was gluing the ID at the end of the url which was what was needed i did forget to add the single thing i changed in the code but that was like 5 characters  [.... ]

What do you think this line of code does  :

 

require_once '/var/www/wits.ruc.dk/db.php'; 

 

It includes the file db.php  which contains the functions which connect to a database  (mysql, mariadb,postgressql,sqlite, doesn't matter), and probably your functions get_uids()  and get_user() 

 

Where do you think you retrieve the UIDs  and user information from? It's stored in a database.

 

Download the db.php file to your computer, and look inside the code on how the SQL queries are formed by those functions.

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

On 10/3/2023 at 5:52 PM, mariushm said:

What do you think this line of code does  :

 

require_once '/var/www/wits.ruc.dk/db.php'; 

 

It includes the file db.php  which contains the functions which connect to a database  (mysql, mariadb,postgressql,sqlite, doesn't matter), and probably your functions get_uids()  and get_user() 

 

Where do you think you retrieve the UIDs  and user information from? It's stored in a database.

 

Download the db.php file to your computer, and look inside the code on how the SQL queries are formed by those functions.

 

 

i know what that file does i know its connected to a database and what it contains but me looking inside it will not help me im not allowed to use sql im only allowed to use the specific things like get_uids() and such which the school has provided and then normal php 

you gotta remember im in school so i have to go by the rules the university sets i dont just have free wings to solve the problem however i want 

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

×