0

I'm trying to do links like: test.com/article.php?=$id where $id is from data base

  $sql = "SELECT * FROM articles WHERE writer='$w_name' ";
   $result = mysqli_query($conn, $sql);

   $q = array();
   $l = array();

   if (mysqli_num_rows($result) > 0) {

       while ($row = mysqli_fetch_assoc($result)) {
           $q[]=$row['header'];
           $l[]=$row['id'];
       }
   }

So i have for example: $q=1,2,3 and $l=1st link name,2nd link name, 3d link name.

I tried to do that together this way, but it doesn't work correct:

<? 
    foreach($q as $header)
        { 
            echo'<li>'.$header.'</li>';
        } 
?>


<?
    foreach($l as $id) 
        { 
            echo "<a href='article.php?=$id'>".$header.'<br>'.'</a>'; 
         }
?>

In this case i had 3 different links with the same name like:

<a href='article.php?=1'>1st link name</a>
<a href='article.php?=2'>1st link name</a>
<a href='article.php?=3'>1st link name</a>

Also i have tried :

 <? 
foreach($l as $id)
foreach($q as $header) 
{ echo "<a href='article.php?=$id'>".$header.'<br>'.'</a>'; } ?>



But it gave me all combinations like:

<a href='article.php?=1'>1st link name</a>
<a href='article.php?=1'>2nd link name</a>
<a href='article.php?=1'>3d link name</a>

<a href='article.php?=2'>1st link name</a>
<a href='article.php?=2'>2nd link name</a>
<a href='article.php?=2'>3d link name</a>

<a href='article.php?=3'>1st link name</a>
<a href='article.php?=3'>2nd link name</a>
<a href='article.php?=3'>3d link name</a>

I'm new to php and mysql, and i don't know how to solve this problem

1
  • Q: Why would you want to have it return ?=xxx? Get methods require something like ?var=xxx - I'm wondering how you're going to be accessing those links. I.e.: if(isset($_GET['xxx']) && ($_GET['xxx'] == "1")){...} Commented Feb 5, 2015 at 20:45

3 Answers 3

2

A foreach loop is best at iterating through a single array. If you insist on having the data in 2 arrays it is easier to iterate the arrays using a for loop with an index instead of a foreach loop.

So try this:

for($i=0; $i < count($l); $i++) {
  echo "<a href='article.php?=".$l[$i]."'>".$q[$i].'<br>'.'</a>';
}

In this example the data is accessed using the index $i with $l[$i] and $q[$i].

Sign up to request clarification or add additional context in comments.

5 Comments

or just build ONE array, since the headers and ids seem to be tied together at the hip...
you may be right, I just thought that the opt has his reasons to split the data in 2 arrays
It gives me all combinations again. 9 links instead 3
@Misa, please take care that there is only one for loop
I'd like to see more explanation of your solution. A code dump is less helpful to future coders.
1

You just need 1 array:

$sql = "SELECT * FROM articles WHERE writer='$w_name' ";
   $result = mysqli_query($conn, $sql);

   $q = array();

   if (mysqli_num_rows($result) > 0) {

       while ($row = mysqli_fetch_assoc($result)) {
           $q[$row['id']]=$row['header'];
       }
   }

After that just print the array in the format you want:

foreach($q as $href => $text) {
    echo '<a href="http://test.com/article.php?=' . $href . '">' . $text . '</a><br />';
}

Comments

0

Solution:

This soltion will consume less memory and efficient.

You can use it like this. Let say you have id and header fields in articles table. By using single iteration(loop) you can achieve your desired result.

$sql = "SELECT id,header FROM articles WHERE writer='$w_name' ";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
   while ($row = mysqli_fetch_assoc($result)) {
       $id = $row['id'];
       echo "<a href='article.php?=$id'>" . $row['header'] . '</a>';
       echo '<br />';
   }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.