Jump to content

Recommended Posts

I'm not even sure the correct terminology. I have a foreach loop that contains details about different automobiles.  I have another page with a list of automobiles and each one will link to the contents that is pulled from each loop on the details page.  How do I make each loop on the details page its own page with a unique id so its not all appearing on one page.  

 

This is my loop and I have another page that I want to lead to each of these individually.

<?php
 
error_reporting(-1);
 
 
$preowned = simplexml_load_file('file.xml');
foreach ($preowned as $preownedinfo)
{
    $price = '$' . money_format($preownedinfo->Price, 2);
 
    $photosHTML = '';
    foreach ($preownedinfo->AditionalPhotos->AditionalPhoto as $addphoto)
    {
        $photosHTML .= "<li style=\"float:left; margin:0 2px;\"><img src=\"$addphoto\" width=\"96\" /></li>";
    }
 
    echo "
<div class=\"detailsTitle\">
    <a href=\"#\">{$preownedinfo->Yrs} {$preownedinfo->Make} {$preownedinfo->Model} {$preownedinfo->ExtraField->ContentEN->ExteriorColor} {$preownedinfo->ExtraField->ContentEN->Doors} Doors</a>
</div>    
<div>
    <div style=\"text-align:center\"><img src=\"{$preownedinfo->MainPhoto}\"width=\"400\" border=\"0\" /></div>
    <div style=\"margin-top:10px;\">
        <ul style=\"list-style:none; display:inline;\">$photosHTML</ul>
    </div>
    <div style=\"clear:both;\"></div>
        <div class=\"specsDetails\">
            <div class=\"specsDetailsRow\">
                <div class=\"leftDetailsSpec\">Our Price: </div>
                <div class=\"rightSpec\">{$price}</div>
            </div>
            <div class=\"specsDetailsRow\">
                <div class=\"leftDetailsSpec\">Body Style: </div>
                <div class=\"rightSpec\">{$preownedinfo->Trim}</div>
            </div>
            <div class=\"specsDetailsRow\">
                <div class=\"leftDetailsSpec\">Status: </div>
                <div class=\"rightSpec\">Used</div>
            </div>
            <div class=\"specsDetailsRow\">
                <div class=\"leftDetailsSpec\">Engine: </div>
                <div class=\"rightSpec\">{$preownedinfo->ExtraField->ContentEN->Engine}</div>
            </div>
            <div class=\"specsDetailsRow\">
                <div class=\"leftDetailsSpec\">Transmission: </div>
                <div class=\"rightSpec\">{$preownedinfo->ExtraField->ContentEN->Transmission}</div>
            </div>
            <div class=\"specsDetailsRow\">
                <div class=\"leftDetailsSpec\">Ext. Colour: </div>
                <div class=\"rightSpec\">{$preownedinfo->ExtraField->ContentEN->ExteriorColor}</div>
            </div>
            <div class=\"specsDetailsRow\">
                <div class=\"leftDetailsSpec\">Int. Colour: </div>
                <div class=\"rightSpec\">{$preownedinfo->ExtraField->ContentEN->InteriorColor}</div>
            </div>
            <div class=\"specsDetailsRow\">
                <div class=\"leftDetailsSpec\">Klometres: </div>
                <div class=\"rightSpec\">{$preownedinfo->ExtraField->ContentEN->Odometer}</div>
            </div>
            <div class=\"specsDetailsRow\">
                <div class=\"leftDetailsSpec\">Stock Number: </div>
                <div class=\"rightSpec\">{$preownedinfo->StockNumber}</div>
            </div>
    </div>
    <div class=\"detailsOverview\">{$preownedinfo->AdDescription}</div>
    <div class=\"detailsOverview requestInfo\">
        <a href=\"#\">Request Info</a>
    </div>
</div>";
 
}
 
?> 
Edited by kawaii1001
Link to comment
https://forums.phpfreaks.com/topic/281668-creating-new-page-from-foreach-loop/
Share on other sites

xml is intended as a data exchange format between different systems, not a database. if your xml data doesn't already have a unique way of identifying each different block of data (or the .xml file will never change, which is unlikely), you would need to alter your xml data format to add a unique identifier to each block of data.

this will be a much simpler task, taking less code and effort on your part, and run much faster, if you store this data in a database so that you can let the database assign and manage a unique id for each different piece of data.

Each block in the xml file does actually have an ID.  For example:

<root>
<AD>
<ADID displayName="ADID">11230394</ADID>
<CompanyID displayName="CompanyID">1235343</CompanyID>
<CompanyName displayName="CompanyName">Their name</CompanyName>
<Category displayName="Category">Sport Utility</Category>
<StockNumber displayName="StockNumber">j00645</StockNumber>
<Vin displayName="Vin">3335</Vin>
<Status displayName="Status">Used</Status>
<Yrs displayName="Year">2009</Yrs>
<Make displayName="Make">Lincoln</Make>
<Model displayName="Model">Navigator</Model>
<Trim displayName="Trim">Sport Utility 4D</Trim>...

So the ADID would be the ID for each block.  Does this help...

Edited by kawaii1001

 Does this help...

 

i don't know, did it help you when you tried to produce the links with the ADID values in them and to find just the data in the .xml file that matches the submitted ADID value? if you identified that your data has a usable value in it, that's just the first step. the next step would be to actually try to solve this yourself.

 

the other forum members here are not the ones who are trying/need to do this, you are.

I'm sorry.  I'm just so lost.  I really am just trying to figure out php.  I was assigned this task and I'm just not even sure the terminology to google.  I know that identifier is in the xml file, but even if I set up a link to grab using that, it wouldn't get all of the information in that loop.  I was just provided the xml.

the only loop you would (should) have is the one looping over the data to build the links. to find/retrieve the matching element in the xml data, you would use an xpath query to search for it -

$preowned = simplexml_load_file('file.xml');

$ADID = isset($_GET['ADID']) ? $_GET['ADID'] : false; // the ADID to search for

// test if there is a search term
if(!$ADID){
    echo 'No search specified';
} else {
    // search for the element with the given ADID value
    $result = $preowned->xpath("/root/AD[ADID=$ADID]");
    if(empty($result)){
        echo 'Not found';
    } else {
        $preownedinfo = $result[0]; // get the first (only) element
    
        // display the information from the element that matched the ADID search
        
    }
}
Edited by mac_gyver
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.