使PHP数组结果显示在页面的不同区域

时间:2014-04-22 17:43:00

标签: php arrays hyperlink

我创建了以下代码,如果链接已经存在,则将ezproxy代码添加到链接中。我的页面上有6个链接,4个已经预先添加,2个没有。将前置添加到2个链接没有它工作正常,但我如何调用结果来替换不包含前缀的链接?

这是我的页面:http://lis7440.slis.wayne.edu/eh3343/final/electronic.php

我需要用阵列结果替换ACP Pier和BioMed链接。我用来预装的代码是:

<?php



 $array = array("http://proxy.lib.wayne.edu/login?url=http://accessanesthesiology.mhmedical.com", "http://proxy.lib.wayne.edu/login?url=http://accessmedicine.mhmedical.com",
 "http://proxy.lib.wayne.edu/login?url=http://accesspharmacy.mhmedical.com", "http://proxy.lib.wayne.edu/login?url=http://accesssurgery.mhmedical.com", 
 "http://online.statref.com/Default.aspx?grpalias=MLC", "http://www.biomedcentral.com");



foreach($array as $value) {
    $pattern = '/^http?:\/\/proxy\.lib\.wayne\.edu\/login\?url=/';
    if (preg_match($pattern, $value)) {

    echo "<a href=$value>Database</a>";
    }
    else{
    $value = "http://proxy.lib.wayne.edu/login?url=".$value;

    }
    echo "<a href=$value>Database</a>";
    }
    ?>

如何使用新的前置阵列结果链接替换这2个链接(段落部分中的ACP Pier和BioMed)?

感谢您的任何建议。

整个代码是:

<!<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<html>

<head>
<title>Medical Library</title>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<link rel="stylesheet" type="text/css" href="library.css" />


</head>

<body>
<div id="wrapper">

    <div id="header">
        <h1>Electronic Resources</h1>
    </div>




    <div id="maincontent">

    <div class="divimg">
    <img src="medical.png" alt="Medical">
    </div>





<p><a href="http://proxy.lib.wayne.edu/login?url=http://accessanesthesiology.mhmedical.com">AccessAnesthesiology (McGraw Hill)</a></p>

<p>In AccessAnesthesiology, physicians and residents can access a compendium of medical resources, including exclusive procedural videos, 
Custom Curriculum, and power search functionality, blended seamlessly with tables and charts designed for quick reference. Backed by an 
esteemed advisory board, AccessAnesthesiology offers multiple tools for resident education and certification/recertification in pain 
managements, critical care, and perioperative medicine.</p>

<p><a href="http://proxy.lib.wayne.edu/login?url=http://accessmedicine.mhmedical.com">AccessMedicine</a></p>

<p>A repository of medical knowledge from internal medicine, cardiology, genetics, pharmacy, diagnosis and management, basic sciences, 
patient care, and more. Continuously expanding, all databases in the repository contain the latest editions of selected medical titles.</p>

<p><a href="http://proxy.lib.wayne.edu/login?url=http://accesspharmacy.mhmedical.com">AccessPharmacy (AccessPharmacy)</a></p>

<p>Users can search over 20 core curriculum textbooks, identify a core topic, or browse site by organ system. Case studies, 
care plans, self-assessments and NAPLEX review help students develop and track key skills providing critical feedback for students and teachers. 
Other features include information on clinical relevance and lab tests, comprehensive drug database, news & updates from Pharmacotherapy News 
Network (PNN) and Goodman & Gilmans Pharmacological Basis of Therapeutics.</p>

<p><a href="http://proxy.lib.wayne.edu/login?url=http://accesssurgery.mhmedical.com">AccessSurgery (McGraw-Hill)</a></p>

<p>McGraw-Hill's AccessSurgery.com is an integrated online resource that provides medical students, surgical residents, and practicing 
surgeons with quick answers to surgical inquiries from trusted sources. Organized around the ACGME's (Accreditation Council for Graduate 
Medical Education) mandate for a Core Curriculum, AccessSurgery delivers content in context.</p>

<p><a href="http://online.statref.com/Default.aspx?grpalias=MLC">ACP Pier: The physician information and education resource (STATRef)</a></p>

<p>PIER is evidence-based clinical guidance presented electronically in a unique layered and telegraphic format. 
It is designed for rapid access to clinical information at the point of care. PIER includes recommendations based 
on all levels of medical evidence including RCTs, cohort and observational studies, case reports, and expert opinion.</p>

<p><a href="http://www.biomedcentral.com">BioMed Central (Biomed Central)</a></p>

<p>BioMed Central Ltd., an independent publisher in London, England, provides free access to biomedical research publications. 
These publications include biology and medicine journal articles, current reports, and meeting abstracts. BioMed Central offers
 information about current controlled trials, as well as topics in modern biology.</p>

 <?php



 $array = array("http://proxy.lib.wayne.edu/login?url=http://accessanesthesiology.mhmedical.com", "http://proxy.lib.wayne.edu/login?url=http://accessmedicine.mhmedical.com",
 "http://proxy.lib.wayne.edu/login?url=http://accesspharmacy.mhmedical.com", "http://proxy.lib.wayne.edu/login?url=http://accesssurgery.mhmedical.com", 
 "http://online.statref.com/Default.aspx?grpalias=MLC", "http://www.biomedcentral.com");



foreach($array as $value) {
    $pattern = '/^http?:\/\/proxy\.lib\.wayne\.edu\/login\?url=/';
    if (preg_match($pattern, $value)) {

    echo "<a href=$value>Database</a>";
    }
    else{
    $value = "http://proxy.lib.wayne.edu/login?url=".$value;

    }
    echo "<a href=$value>Database</a>";
    }
    ?>


</body>
</html>

1 个答案:

答案 0 :(得分:0)

您只需在IF

中添加第二个回音
$array = array("http://proxy.lib.wayne.edu/login?url=http://accessanesthesiology.mhmedical.com", "http://proxy.lib.wayne.edu/login?url=http://accessmedicine.mhmedical.com","http://proxy.lib.wayne.edu/login?url=http://accesspharmacy.mhmedical.com", "http://proxy.lib.wayne.edu/login?url=http://accesssurgery.mhmedical.com", "http://online.statref.com/Default.aspx?grpalias=MLC", "http://www.biomedcentral.com");

foreach($array as $value) {
$pattern = '/^http?:\/\/proxy\.lib\.wayne\.edu\/login\?url=/';
    if (preg_match($pattern, $value)) {
        echo "<a href=$value>Database</a>";
    }
    else{
        $value = "http://proxy.lib.wayne.edu/login?url=".$value;
        echo "<a href=$value>Database</a>";
    }
}

使用虚拟数据库编辑添加的PHP解决方案

<?php

$resources = array();
// Add Resources to Array (fake DB)
array_push($resources, array(
    'link' => 'http://proxy.lib.wayne.edu/login?url=http://accessanesthesiology.mhmedical.com',
    'title' => 'AccessAnesthesiology (McGraw Hill)',
    'text' => 'In AccessAnesthesiology, physicians and residents can access a compendium of medical resources, including exclusive procedural videos, 
    Custom Curriculum, and power search functionality, blended seamlessly with tables and charts designed for quick reference. Backed by an 
    esteemed advisory board, AccessAnesthesiology offers multiple tools for resident education and certification/recertification in pain 
    managements, critical care, and perioperative medicine.'
));

array_push($resources, array(
    'link' => 'http://proxy.lib.wayne.edu/login?url=http://accessmedicine.mhmedical.com',
    'title' => 'AccessMedicine',
    'text' => 'A repository of medical knowledge from internal medicine, cardiology, genetics, pharmacy, diagnosis and management, basic sciences, 
    patient care, and more. Continuously expanding, all databases in the repository contain the latest editions of selected medical titles.'
));

array_push($resources, array(
    'link' => 'http://proxy.lib.wayne.edu/login?url=http://accesspharmacy.mhmedical.com',
    'title' => 'AccessPharmacy (AccessPharmacy)',
    'text' => 'Users can search over 20 core curriculum textbooks, identify a core topic, or browse site by organ system. Case studies, 
    care plans, self-assessments and NAPLEX review help students develop and track key skills providing critical feedback for students and teachers. 
    Other features include information on clinical relevance and lab tests, comprehensive drug database, news & updates from Pharmacotherapy News 
    Network (PNN) and Goodman & Gilmans Pharmacological Basis of Therapeutics.'
));

array_push($resources, array(
    'link' => 'http://proxy.lib.wayne.edu/login?url=http://accesssurgery.mhmedical.com',
    'title' => 'AccessSurgery (McGraw-Hill)',
    'text' => "McGraw-Hill's AccessSurgery.com is an integrated online resource that provides medical students, surgical residents, and practicing 
    surgeons with quick answers to surgical inquiries from trusted sources. Organized around the ACGME's (Accreditation Council for Graduate 
    Medical Education) mandate for a Core Curriculum, AccessSurgery delivers content in context."
));

array_push($resources, array(
    'link' => 'http://online.statref.com/Default.aspx?grpalias=MLC',
    'title' => 'ACP Pier: The physician information and education resource (STATRef)',
    'text' => 'PIER is evidence-based clinical guidance presented electronically in a unique layered and telegraphic format. 
    It is designed for rapid access to clinical information at the point of care. PIER includes recommendations based 
    on all levels of medical evidence including RCTs, cohort and observational studies, case reports, and expert opinion.'
));

array_push($resources, array(
    'link' => 'http://www.biomedcentral.com',
    'title' => 'BioMed Central (Biomed Central)',
    'text' => 'BioMed Central Ltd., an independent publisher in London, England, provides free access to biomedical research publications. 
    These publications include biology and medicine journal articles, current reports, and meeting abstracts. BioMed Central offers
     information about current controlled trials, as well as topics in modern biology.'
));

?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<html>

<head>
<title>Medical Library</title>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<link rel="stylesheet" type="text/css" href="library.css" />


</head>

<body>
<div id="wrapper">

    <div id="header">
        <h1>Electronic Resources</h1>
    </div>

    <div id="maincontent">

    <div class="divimg">
    <img src="medical.png" alt="Medical">
    </div>


<?php foreach ($resources as $resource) : ?>

    <?php 
    $pattern = '/^http?:\/\/proxy\.lib\.wayne\.edu\/login\?url=/';
    if (!preg_match($pattern, $resource['link'])) $resource['link'] = "http://proxy.lib.wayne.edu/login?url=" . $resource['link'];
    ?>

    <p><a href="<?php echo $resource['link']?>"><?php echo $resource['title'] ?></a></p>

    <p><?php echo $resource['text']; ?></p>

<?php endforeach; ?>

</body>
</html>