Skip to main content
Inspiring
January 5, 2014
Answered

How to provide a link from a resultset variable?

  • January 5, 2014
  • 2 replies
  • 2185 views

I have a list of id and name pairs in a database table using Mysqli.

The PHP code that prints them out is as follows:

while( $row = mysqli_fetch_array( $resultset, MYSQLI_NUM))

{ echo $row[0] ." - ". $row[1] ."\n <br/ >";

}

This works fine. There are up to 6000 items in this list, so it uses a repeat region (not shown).

I want to link to a page that will use the id to do things with it (like edit or delete a record).

I need to send the value of $row[0] to the next page, but use $row[1] as a link to a page called testlink.php.

The following code:

<a href="testlink.php?ref=<?php echo $row[0]; ?>"><?php echo $row[1];?> </a>

seems like a way to do it, but I cannot figure just how to insert this into the echo line without getting errors.

Can anyone help with the php code to do this?

And is there a better way to access individual items in very large lists other than using what is in effect a multi-page drop down menu.

Howard Walker

This topic has been closed for replies.
Correct answer bregent

>This produces what I need, but the id index is not printed before the linked name.

Do you mean the id is not being inserted into the querystring?

Looks like you have an extra quote mark in there. Try this

<a href="testlink.php?ref=<?php echo $row['id']; ?>"><?php echo $row['company']."\n"."<br>"; }?></a><br />

2 replies

Participating Frequently
January 6, 2014

Your idea looks good, probably just a syntax error. I'm not sure but possibly you need whitespace before the last closing php '?>"

<a href="testlink.php?ref=<?php echo $row[0]; ?>"><?php echo $row[1]; ?> </a>

>And is there a better way to access individual items in very large

>lists other than using what is in effect a multi-page drop down menu.

If at all possible, allow the user to filter the list down to a reasonable number of items; 6000 is too much

Inspiring
January 6, 2014

When you have a list of 6000, the only sensible way to filter them down I originally thought was to use the first letter.

However, I have now found that if I use mysqli to select all items containing a few (say 4 ) letters, I can usually get the list down to less than 5 items, display a list of these and then just select one of them.

See http://tyneships2.co.uk/zebras/word_search.php

This site was moved to a version 5.5 server when it was written in mysql and as a result I am having to rewrite it. Its a bit scrappy, but works after a fashion, unlike the original site.

This selects and displays all items with the letters in the name fairly quickly, but there are only about 4000 items to look through, with more being added daily.

Later I will use the shortlist to pick the final one prior to displaying it.

Try entering sal sall and sally. if you have a lot of returned pages, like you have for sal, then depending on your connection speed, you could be waiting ages, but if you use sally, then you have only one and that is instant, even at my slow speed. So like you say, it will be better to filter using word search and then pick the right one.

Only problem is that your first search may not find a result.

Thanks for your input.

Braniac
January 6, 2014

Not really following.

Are you trying to return the names and ids in a list and create a link to a delete or edit page?

if so I would just query the database like so:

<?php

$conn = new mysqli('localhost' , 'username' , 'password' , 'databaseName');

$getNameList = $conn->("SELECT * FROM tablename") or die ($conn->error);

<?php

<?php while ($row = $getNameList->fetch_assoc()) { ?>

<p><a href="textlink.php?ref="<?php echo $row['id']; ?>"><?php echo $row['name']; ?></a></p>

<?php } ? >

Inspiring
January 6, 2014

Thanks a lot Osgood. It did cause many syntax errors, but it gave me the hint:

This is my code at the mpoment, and it works as needed.

$conn = mysqli_connect( $DatabaseServer, $DatabaseUser, $DatabasePassword, $DatabaseName);

$sql ='SELECT id, company FROM companies';

$resultset = mysqli_query( $conn, $sql);

  while ($row = $resultset->fetch_assoc()) { ?>

<a href="testlink.php?ref="<?php echo $row['id']; ?>"><?php echo $row['company']."\n"."<br>"; }?></a><br />

This produces what I need, but the id index is not printed before the linked name.

Why did you use new mysqli instead of just mysqli.?

Howard

bregentCorrect answer
Participating Frequently
January 6, 2014

>This produces what I need, but the id index is not printed before the linked name.

Do you mean the id is not being inserted into the querystring?

Looks like you have an extra quote mark in there. Try this

<a href="testlink.php?ref=<?php echo $row['id']; ?>"><?php echo $row['company']."\n"."<br>"; }?></a><br />